Deleting Objects (Blobs) from Azure Blob Storage
This document outlines the methods for deleting objects (blobs) from Azure Blob Storage using various tools and SDKs.
Important: Blob deletion is a permanent operation. Once a blob is deleted, it cannot be recovered unless soft delete is enabled for the container.
Prerequisites
- An Azure Storage account.
- A container within your storage account.
- The name of the blob you wish to delete.
- Appropriate authentication credentials (e.g., connection string, SAS token, or Azure AD credentials).
Methods for Deleting Blobs
1. Using Azure CLI
The Azure Command-Line Interface (CLI) provides a straightforward way to manage your Azure resources, including deleting blobs.
Deleting a single blob
az storage blob delete \
--account-name \
--container-name \
--name \
--account-key # Or use --connection-string or Azure AD authentication
Replace placeholders like <your-storage-account-name>, <your-container-name>, <your-blob-name>, and <your-account-key> with your specific values.
Deleting multiple blobs (using a prefix)
You can delete all blobs that start with a specific prefix.
az storage blob delete-batch \
--account-name \
--container-name \
--pattern "my-prefix-*" \
--account-key
The --pattern argument supports wildcard characters. For example, "my-prefix-*" will delete all blobs whose names start with "my-prefix-".
2. Using Azure PowerShell
Azure PowerShell offers cmdlets for managing blob storage.
Deleting a single blob
Remove-AzStorageBlob `
-ConnectionString "" `
-ContainerName "" `
-Blob ""
Ensure you have the Azure PowerShell module installed and are logged in.
Deleting multiple blobs
You can use Get-AzStorageBlob with a filter and pipe it to Remove-AzStorageBlob.
Get-AzStorageBlob `
-ConnectionString "" `
-ContainerName "" `
-Prefix "old-files/" `
| Remove-AzStorageBlob
3. Using Azure SDKs (e.g., .NET, Python, Java, Node.js)
The Azure SDKs provide programmatic control for deleting blobs within your applications.
Example: Python SDK
from azure.storage.blob import BlobServiceClient
connect_str = ""
container_name = ""
blob_name = ""
# Create the BlobServiceClient object
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
# Get a client to interact with a specific blob
blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name)
try:
# Delete the blob
blob_client.delete_blob()
print(f"Blob '{blob_name}' deleted successfully from container '{container_name}'.")
except Exception as e:
print(f"Error deleting blob: {e}")
Example: .NET SDK
using Azure.Storage.Blobs;
using System;
string connectionString = "";
string containerName = "";
string blobName = "";
// Create a BlobServiceClient object
BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
// Get a client to interact with a specific blob
BlobClient blobClient = blobServiceClient.GetBlobClient(blobName);
try
{
// Delete the blob
var response = await blobClient.DeleteAsync();
Console.WriteLine($"Blob '{blobName}' deleted successfully from container '{containerName}'.");
}
catch (Exception ex)
{
Console.WriteLine($"Error deleting blob: {ex.Message}");
}
Refer to the official Azure SDK documentation for your preferred language for detailed examples and advanced options, such as conditional deletion or deleting snapshots.
Important Considerations
- Soft Delete: If soft delete is enabled for your container, deleting a blob will mark it as deleted but retain it for a specified retention period. You can recover soft-deleted blobs within this period.
- Immutability Policies: If an immutability policy is applied to a container or blob, you may not be able to delete the blob until the policy expires.
- Lease: If a blob has an active lease, you must break the lease before you can delete it.
- Permissions: Ensure the identity you are using (user, service principal, managed identity) has the necessary permissions (e.g., "Microsoft.Storage/storageAccounts/blobServices/containers/delete" action) to delete blobs.