Delete and restore a blob container with .NET

This article shows how to delete containers with the Azure Storage client library for .NET. 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 .NET. To learn about setting up your project, including package installation, adding using directives, and creating an authorized client object, see Get started with Azure Blob Storage and .NET.
  • 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 .NET, use one of the following methods:

The Delete and DeleteAsync methods throw an exception if the container doesn't exist.

The DeleteIfExists and DeleteIfExistsAsync methods return a Boolean value indicating whether the container was deleted. If the specified container doesn't exist, then these methods return False to indicate that the container wasn't deleted.

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 deletes the specified container, and handles the exception if the container doesn't exist:

//-------------------------------------------------
// Delete a container
//-------------------------------------------------
private static async Task DeleteSampleContainerAsync(BlobServiceClient blobServiceClient, string containerName)
{
    BlobContainerClient container = blobServiceClient.GetBlobContainerClient(containerName);

    try
    {
        // Delete the specified container and handle the exception.
        await container.DeleteAsync();
    }
    catch (RequestFailedException e)
    {
        Console.WriteLine("HTTP error code {0}: {1}",
                            e.Status, e.ErrorCode);
        Console.WriteLine(e.Message);
        Console.ReadLine();
    }
}

The following example shows how to delete all of the containers that start with a specified prefix.

//-------------------------------------------------
// Delete all containers with the specified prefix
//-------------------------------------------------
private static async Task DeleteContainersWithPrefixAsync(BlobServiceClient blobServiceClient, string prefix)
{
    Console.WriteLine("Delete all containers beginning with the specified prefix");

    try
    {
        foreach (BlobContainerItem container in blobServiceClient.GetBlobContainers())
        {
            if (container.Name.StartsWith(prefix))
            { 
                Console.WriteLine("\tContainer:" + container.Name);
                BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(container.Name);
                await containerClient.DeleteAsync();
            }
        }

        Console.WriteLine();
    }
    catch (RequestFailedException e)
    {
        Console.WriteLine(e.Message);
        Console.ReadLine();
        throw;
    }
}

Restore a deleted container

When container soft delete is enabled for a storage account, a container and its contents may be recovered after it has been deleted, within a retention period that you specify. You can restore a soft-deleted container by calling either of the following methods of the BlobServiceClient class.

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

public static async Task RestoreContainer(BlobServiceClient client, string containerName)
{
    await foreach (BlobContainerItem item in client.GetBlobContainersAsync
        (BlobContainerTraits.None, BlobContainerStates.Deleted))
    {
        if (item.Name == containerName && (item.IsDeleted == true))
        {
            try 
            { 
                await client.UndeleteBlobContainerAsync(containerName, item.VersionId);
            }
            catch (RequestFailedException e)
            {
                Console.WriteLine("HTTP error code {0}: {1}",
                e.Status, e.ErrorCode);
                Console.WriteLine(e.Message);
            }
        }
    }
}

Resources

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

REST API operations

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

Client library resources

See also