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

Set up your environment

If you don't have an existing project, this section shows you how to set up a project to work with the Azure Blob Storage client library for Java. For more information, see Get started with Azure Blob Storage and Java.

To work with the code examples in this article, follow these steps to set up your project.

Note

This article uses the Maven build tool to build and run the example code. Other build tools, such as Gradle, also work with the Azure SDK for Java.

Install packages

Open the pom.xml file in your text editor. Install the packages by including the BOM file, or including a direct dependency.

Add import statements

Add the following import statements:

import com.azure.storage.blob.*;
import com.azure.storage.blob.models.*;

Authorization

The authorization mechanism must have the necessary permissions to delete or restore a container. For authorization with Microsoft Entra ID (recommended), you need Azure RBAC built-in role Storage Blob Data Contributor or higher. To learn more, see the authorization guidance for Delete Container (REST API) and Restore Container (REST API).

Create a client object

To connect an app to Blob Storage, create an instance of BlobServiceClient.

The following example uses BlobServiceClientBuilder to build a BlobServiceClient object using DefaultAzureCredential, and shows how to create container and blob clients, if needed:

// Azure SDK client builders accept the credential as a parameter
// TODO: Replace <storage-account-name> with your actual storage account name
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
        .endpoint("https://<storage-account-name>.blob.core.windows.net/")
        .credential(new DefaultAzureCredentialBuilder().build())
        .buildClient();

// If needed, you can create a BlobContainerClient object from the BlobServiceClient
BlobContainerClient containerClient = blobServiceClient
        .getBlobContainerClient("<container-name>");

// If needed, you can create a BlobClient object from the BlobContainerClient
BlobClient blobClient = containerClient
        .getBlobClient("<blob-name>");

To learn more about creating and managing client objects, see Create and manage client objects that interact with data resources.

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.

Code samples

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:

Client library resources

See also

  • This article is part of the Blob Storage developer guide for Java. To learn more, see the full list of developer guide articles at Build your Java app.