Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit editor mode

Delete and restore a blob container with JavaScript or TypeScript

Feedback

In this article

This article shows how to delete containers with theAzure Storage client library for JavaScript. If you've enabledcontainer 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, seeGet started with Azure Blob Storage and JavaScript.
  • Theauthorization 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 theBlobServiceClient class:

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

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

The following example uses aBlobServiceClient object to delete the specified container:

async function deleteContainer(blobServiceClient, containerName) {    return await blobServiceClient.deleteContainer(containerName);}

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

async function deleteContainersWithPrefix(blobServiceClient, prefix) {  const containerOptions = {    includeDeleted: false,    includeMetadata: false,    includeSystem: true,    prefix  }  for await (const containerItem of blobServiceClient.listContainers(containerOptions)) {    try{      const containerClient = blobServiceClient.getContainerClient(containerItem.name);      await containerClient.delete();        console.log(`Deleted ${containerItem.name} container - success`);    }catch(ex){      console.log(`Deleted ${containerItem.name} container - failed - ${ex.message}`);    }  }}

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 aBlobServiceClient object:

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

async function undeleteContainer(blobServiceClient, containerName) {    // Version to restore  let containerVersion;  const containerOptions = {    includeDeleted: true,    prefix: containerName  }  // Find the deleted container and restore it  for await (const containerItem of blobServiceClient.listContainers(containerOptions)) {    if (containerItem.name === containerName) {      containerVersion = containerItem.version;    }  }  const containerClient = await blobServiceClient.undeleteContainer(    containerName,    containerVersion,  );}

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:

Client library resources

See also

Related content

  • This article is part of the Blob Storage developer guide for JavaScript/TypeScript. To learn more, see the full list of developer guide articles atBuild your JavaScript/TypeScript app.

Feedback

Was this page helpful?

YesNoNo

Need help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?

  • Last updated on

In this article

Was this page helpful?

YesNo
NoNeed help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?