Delete and restore a blob container with JavaScript or TypeScript - Azure Storage (original) (raw)

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

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:

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 <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mrow><mi>c</mi><mi>o</mi><mi>n</mi><mi>t</mi><mi>a</mi><mi>i</mi><mi>n</mi><mi>e</mi><mi>r</mi><mi>I</mi><mi>t</mi><mi>e</mi><mi>m</mi><mi mathvariant="normal">.</mi><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></mrow><mi>c</mi><mi>o</mi><mi>n</mi><mi>t</mi><mi>a</mi><mi>i</mi><mi>n</mi><mi>e</mi><mi>r</mi><mo>−</mo><mi>f</mi><mi>a</mi><mi>i</mi><mi>l</mi><mi>e</mi><mi>d</mi><mo>−</mo></mrow><annotation encoding="application/x-tex">{containerItem.name} container - failed - </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.7667em;vertical-align:-0.0833em;"></span><span class="mord"><span class="mord mathnormal">co</span><span class="mord mathnormal">n</span><span class="mord mathnormal">t</span><span class="mord mathnormal">ain</span><span class="mord mathnormal" style="margin-right:0.02778em;">er</span><span class="mord mathnormal" style="margin-right:0.07847em;">I</span><span class="mord mathnormal">t</span><span class="mord mathnormal">e</span><span class="mord mathnormal">m</span><span class="mord">.</span><span class="mord mathnormal">nam</span><span class="mord mathnormal">e</span></span><span class="mord mathnormal">co</span><span class="mord mathnormal">n</span><span class="mord mathnormal">t</span><span class="mord mathnormal">ain</span><span class="mord mathnormal" style="margin-right:0.02778em;">er</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal">ai</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">e</span><span class="mord mathnormal">d</span><span class="mord">−</span></span></span></span>{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 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.

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