Delete and restore a blob container with Java

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

Prerequisites

  • This article assumes you already have a project set up to work with the Azure Blob Storage client library for Java. To learn about setting up your project, including package installation, adding import directives, and creating an authorized client object, see Get Started with Azure Storage and Java.
  • 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 in Java, use one of the following methods from the BlobServiceClient class:

You can also delete a container using one of the following methods from the BlobContainerClient 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 will fail with HTTP error code 409 (Conflict). Any other operations on the container or the blobs it contains will fail with HTTP error code 404 (Not Found).

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

public void deleteContainer(BlobServiceClient blobServiceClient, String containerName) {
    // Delete the container using the service client
    blobServiceClient.deleteBlobContainer(containerName);
}

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

public void deleteContainersWithPrefix(BlobServiceClient blobServiceClient) {
    ListBlobContainersOptions options = new ListBlobContainersOptions()
            .setPrefix("container-");

    // Delete the container with the specified prefix using the service client
    for (BlobContainerItem containerItem : blobServiceClient.listBlobContainers(options, null)) {
        BlobContainerClient containerClient = blobServiceClient.getBlobContainerClient(containerItem.getName());
        containerClient.delete();
    }
}

Restore a deleted container

When container soft delete is enabled for a storage account, a deleted container and its contents may be recovered within a specified retention period. To learn more about container soft delete, see Enable and manage soft delete for containers. You can restore a soft-deleted container by calling the following method of the BlobServiceClient class:

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

public void restoreContainer(BlobServiceClient blobServiceClient) {
    ListBlobContainersOptions options = new ListBlobContainersOptions();
    options.getDetails().setRetrieveDeleted(true);

    // Delete the container with the specified prefix using the service client
    for (BlobContainerItem deletedContainerItem : blobServiceClient.listBlobContainers(options, null)) {
        BlobContainerClient containerClient = blobServiceClient
                .undeleteBlobContainer(deletedContainerItem.getName(), deletedContainerItem.getVersion());
    }
}

Resources

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

REST API operations

The Azure SDK for Java contains libraries that build on top of the Azure REST API, allowing you to interact with REST API operations through familiar Java paradigms. The client library methods for deleting or restoring a container use the following REST API operations:

Code samples

Client library resources

See also