title | titleSuffix | description | services | author | ms.author | ms.service | ms.topic | ms.date | ms.devlang | ms.custom |
---|---|---|---|---|---|---|---|---|---|---|
Delete and restore a blob container with .NET |
Azure Storage |
Learn how to delete and restore a blob container in your Azure Storage account using the .NET client library. |
storage |
pauljewellmsft |
pauljewell |
azure-blob-storage |
how-to |
03/28/2022 |
csharp |
devx-track-csharp, devguide-csharp, devx-track-dotnet |
[!INCLUDE storage-dev-guide-selector-delete-container]
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.
[!INCLUDE storage-dev-guide-prereqs-dotnet]
[!INCLUDE storage-dev-guide-project-setup-dotnet]
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).
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:
:::code language="csharp" source="~/azure-storage-snippets/blobs/howto/dotnet/dotnet-v12/Containers.cs" id="DeleteSampleContainerAsync":::
The following example shows how to delete all of the containers that start with a specified prefix.
:::code language="csharp" source="~/azure-storage-snippets/blobs/howto/dotnet/dotnet-v12/Containers.cs" id="DeleteContainersWithPrefixAsync":::
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);
}
}
}
}
To learn more about deleting a container using the Azure Blob Storage client library for .NET, see the following resources.
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:
- Delete Container (REST API)
- Restore Container (REST API)
[!INCLUDE storage-dev-guide-resources-dotnet]