Delete a container
This document explains how to delete an Azure Storage container. Deleting a container also deletes all blobs contained within it. This action is irreversible.
Prerequisites
- An Azure Storage account.
- Appropriate permissions to delete containers (e.g., Storage Blob Data Owner or Storage Blob Data Contributor role).
Methods to Delete a Container
1. Using the Azure portal
- Navigate to your storage account in the Azure portal.
- In the left-hand navigation pane, under "Data storage", select "Containers".
- Locate the container you wish to delete.
- Select the container by clicking its name.
- In the container's blade, click the "Delete" button at the top.
- A confirmation dialog will appear. Type the name of the container to confirm deletion and click "Delete".
2. Using Azure CLI
Use the az storage container delete command.
az storage container delete --name mycontainer --account-name mystorageaccount --auth-mode login
Replace mycontainer with the name of your container and mystorageaccount with your storage account name. The --auth-mode login uses your Azure AD credentials.
3. Using Azure PowerShell
Use the Remove-AzStorageContainer cmdlet.
Remove-AzStorageContainer -Name "mycontainer" -Context (Get-AzStorageAccount -ResourceGroupName "myresourcegroup" -Name "mystorageaccount").Context
Replace mycontainer, myresourcegroup, and mystorageaccount with your specific values.
4. Using .NET SDK
You can delete a container programmatically using the Azure Storage client libraries.
using Azure.Storage.Blobs;
using Azure.Identity;
// ...
string accountName = "mystorageaccount";
string containerName = "mycontainer";
// Construct the blob service client with Azure AD authentication
BlobServiceClient blobServiceClient = new BlobServiceClient(
new Uri($"https://{accountName}.blob.core.windows.net"),
new DefaultAzureCredential());
// Get a client that references the container
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
// Delete the container
await containerClient.DeleteAsync();
Console.WriteLine($"Container '{containerName}' deleted successfully.");
5. Using REST API
You can use the Blob Service REST API to delete a container.
DELETE https://mystorageaccount.blob.core.windows.net/mycontainer?restype=container HTTP/1.1
x-ms-version: 2019-02-02
Authorization: SharedKey mystorageaccount:YourAccessKey
Date: Mon, 27 Jul 2024 10:00:00 GMT
Content-Length: 0
Replace mystorageaccount, mycontainer, and YourAccessKey with your actual details. For authentication, you can use Shared Key or Azure AD.
Considerations
- Irreversibility: Deletion is permanent.
- Blobs within container: All blobs in the container are deleted.
- Permissions: Ensure you have the necessary RBAC roles or account keys.
- Leased containers: If a container has an active lease, you must break the lease before deleting it. This is typically done by specifying the
x-ms-lease-action: breakheader in the REST API call or using specific SDK methods.