| title | Delete and restore a blob with JavaScript or TypeScript |
|---|---|
| titleSuffix | Azure Storage |
| description | Learn how to delete and restore a blob in your Azure Storage account using the JavaScript client library |
| services | storage |
| author | stevenmatthew |
| ms.author | shaas |
| ms.date | 10/28/2024 |
| ms.service | azure-blob-storage |
| ms.topic | how-to |
| ms.devlang | javascript |
| ms.custom | devx-track-js, devguide-js, devx-track-ts, devguide-ts |
[!INCLUDE storage-dev-guide-selector-delete-blob]
This article shows how to delete blobs with the Azure Storage client library for JavaScript, and how to restore soft-deleted blobs during the retention period.
- The examples in this article assume you already have a project set up to work with the Azure Blob Storage client library for JavaScript. To learn about setting up your project, including package installation, importing modules, and creating an authorized client object to work with data resources, see Get started with Azure Blob Storage and JavaScript.
- The authorization mechanism must have permissions to delete a blob, or to restore a soft-deleted blob. To learn more, see the authorization guidance for the following REST API operations:
[!INCLUDE storage-dev-guide-delete-blob-note]
To delete a blob, call one of the following methods:
If the blob has any associated snapshots, you must delete all of its snapshots to delete the blob. The following code example shows how to delete a blob and its snapshots:
:::code language="javascript" source="~/azure-storage-snippets/blobs/howto/JavaScript/NodeJS-v12/dev-guide/delete-blob.js" id="snippet_deleteBlob":::
:::code language="typescript" source="~/azure-storage-snippets/blobs/howto/TypeScript/NodeJS-v12/dev-guide/src/blob-delete.ts" id="snippet_deleteBlob":::
Blob soft delete protects an individual blob and its versions, snapshots, and metadata from accidental deletes or overwrites by maintaining the deleted data in the system for a specified period of time. During the retention period, you can restore the blob to its state at deletion. After the retention period expires, the blob is permanently deleted. For more information about blob soft delete, see Soft delete for blobs.
You can use the Azure Storage client libraries to restore a soft-deleted blob or snapshot.
To restore soft-deleted blobs, call the following method:
This method restores soft-deleted blobs and any deleted snapshots associated with it. Calling this method for a blob that hasn't been deleted has no effect.
:::code language="javascript" source="~/azure-storage-snippets/blobs/howto/JavaScript/NodeJS-v12/dev-guide/delete-blob.js" id="snippet_undeleteBlob":::
:::code language="typescript" source="~/azure-storage-snippets/blobs/howto/TypeScript/NodeJS-v12/dev-guide/src/blob-delete.ts" id="snippet_undeleteBlob":::
To learn more about how to delete blobs and restore deleted blobs using the Azure Blob Storage client library for JavaScript, see the following resources.
- View JavaScript and TypeScript code samples from this article (GitHub)
The Azure SDK for JavaScript contains libraries that build on top of the Azure REST API, allowing you to interact with REST API operations through familiar JavaScript paradigms. The client library methods for deleting blobs and restoring deleted blobs use the following REST API operations:
- Delete Blob (REST API)
- Undelete Blob (REST API)
[!INCLUDE storage-dev-guide-resources-javascript]
[!INCLUDE storage-dev-guide-next-steps-javascript]