Skip to content

Latest commit

 

History

History
107 lines (63 loc) · 6.55 KB

File metadata and controls

107 lines (63 loc) · 6.55 KB
title Delete and restore a blob container with JavaScript or TypeScript
titleSuffix Azure Storage
description Learn how to delete and restore a blob container in your Azure Storage account using the JavaScript client library.
services storage
author stevenmatthew
ms.author shaas
ms.service azure-blob-storage
ms.topic how-to
ms.date 10/28/2024
ms.devlang javascript
ms.custom devx-track-js, devguide-js, devx-track-ts, devguide-ts

Delete and restore a blob container with JavaScript or TypeScript

[!INCLUDE storage-dev-guide-selector-delete-container]

This article shows how to delete containers with the Azure Storage client library for JavaScript. If you've enabled container soft delete, you can restore deleted containers.

Prerequisites

  • 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 container, or to restore a soft-deleted container. To learn more, see the authorization guidance for the following REST API operations:

Delete a container

To delete a container, use the following method from the BlobServiceClient class:

You can also delete a container using the following method from the ContainerClient class:

After you delete a container, you can't create a container with the same name for at least 30 seconds. Attempting to create a container with the same name fails with HTTP error code 409 (Conflict). Any other operations on the container or the blobs it contains fail with HTTP error code 404 (Not Found).

The following example uses a BlobServiceClient object to delete the specified container:

:::code language="javascript" source="~/azure-storage-snippets/blobs/howto/JavaScript/NodeJS-v12/dev-guide/delete-containers.js" id="snippet_delete_container_immediately" :::

:::code language="typescript" source="~/azure-storage-snippets/blobs/howto/TypeScript/NodeJS-v12/dev-guide/src/containers-delete.ts" id="snippet_delete_container_immediately" :::


The following example shows how to delete all containers that start with a specified prefix:

:::code language="javascript" source="~/azure-storage-snippets/blobs/howto/JavaScript/NodeJS-v12/dev-guide/delete-containers.js" id="snippet_deleteContainersWithPrefix" :::

:::code language="typescript" source="~/azure-storage-snippets/blobs/howto/TypeScript/NodeJS-v12/dev-guide/src/containers-delete.ts" id="snippet_deleteContainersWithPrefix" :::


Restore a deleted container

When container soft delete is enabled for a storage account, a container and its contents can be recovered after it has been deleted, within a retention period that you specify. You can restore a soft-deleted container using a BlobServiceClient object:

The following example finds a deleted container, gets the version ID of that deleted container, and then passes that ID into the undeleteContainer method to restore the container.

:::code language="javascript" source="~/azure-storage-snippets/blobs/howto/JavaScript/NodeJS-v12/dev-guide/delete-containers.js" id="snippet_undeleteContainer" :::

:::code language="typescript" source="~/azure-storage-snippets/blobs/howto/TypeScript/NodeJS-v12/dev-guide/src/containers-delete.ts" id="snippet_undeleteContainer" :::


Resources

To learn more about deleting a container using the Azure Blob Storage client library for JavaScript, see the following resources.

Code samples

REST API operations

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 or restoring a container use the following REST API operations:

[!INCLUDE storage-dev-guide-resources-javascript]

See also

[!INCLUDE storage-dev-guide-next-steps-javascript]