This document explains how to delete blobs, blob snapshots, and blob versions from Azure Blob Storage.
Azure Blob Storage offers several ways to manage blob deletion:
You can delete blobs using the Azure portal, Azure CLI, Azure PowerShell, or Azure Storage client libraries (SDKs).
The Azure portal provides a user-friendly interface for managing your storage resources.
The Azure Command-Line Interface (CLI) is a powerful tool for scripting storage operations.
To delete a blob:
az storage blob delete \
--account-name <storage-account-name> \
--container-name <container-name> \
--name <blob-name> \
--account-key <storage-account-key>
Replace the placeholders with your actual storage account name, container name, blob name, and account key. You can also use SAS tokens for authentication.
To delete a blob snapshot:
az storage blob delete \
--account-name <storage-account-name> \
--container-name <container-name> \
--name <blob-name> \
--snapshot <snapshot-time> \
--account-key <storage-account-key>
To delete all snapshots of a blob:
az storage blob delete \
--account-name <storage-account-name> \
--container-name <container-name> \
--name <blob-name> \
--delete-snapshots include \
--account-key <storage-account-key>
Azure PowerShell provides cmdlets for managing Azure resources.
To delete a blob:
Remove-AzStorageBlob `
-AccountName <storage-account-name> `
-Container <container-name> `
-Blob <blob-name> `
-Force
To delete all snapshots of a blob:
Remove-AzStorageBlob `
-AccountName <storage-account-name> `
-Container <container-name> `
-Blob <blob-name> `
-IncludeSnapshots `
-Force
You can programmatically delete blobs using the Azure Storage SDKs available for various programming languages.
Example using .NET:
using Azure.Storage.Blobs;
// Replace with your actual connection string
string connectionString = "YOUR_AZURE_STORAGE_CONNECTION_STRING";
string containerName = "mycontainer";
string blobName = "myblob.txt";
BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
BlobClient blobClient = containerClient.GetBlobClient(blobName);
try
{
Response response = await blobClient.DeleteAsync();
if (response.Value)
{
Console.WriteLine($"Blob '{blobName}' deleted successfully.");
}
else
{
Console.WriteLine($"Failed to delete blob '{blobName}'.");
}
}
catch (RequestFailedException e)
{
Console.WriteLine($"Error deleting blob: {e.Message}");
}
To protect against accidental deletion, you can configure:
These features can be configured at the storage account level and provide a critical safety net for your data.
Soft delete can be enabled for blobs, blob snapshots, and blob versions. You can configure the retention period from 1 to 365 days.
When versioning is enabled, every overwrite or delete operation on a blob creates a new version. You can then restore previous versions.