Azure Storage Blob Deletion

Learn how to efficiently delete blobs from your Azure Storage account.

This tutorial guides you through the process of deleting blobs from an Azure Blob Storage container. Deleting blobs is a common operation for managing storage costs and data lifecycle.

Methods for Deleting Blobs

Azure Storage offers several ways to delete blobs, depending on your needs:

Prerequisites

Before you begin, ensure you have the following:

Deleting a Single Blob

You can delete a single blob using the Azure CLI or any of the Azure Storage SDKs.

Using Azure CLI

The following command deletes a blob named my-blob.txt from the container my-container:

Azure CLI
az storage blob delete --container-name my-container --name my-blob.txt --account-name mystorageaccount --account-key YOUR_ACCOUNT_KEY

Replace mystorageaccount and YOUR_ACCOUNT_KEY with your actual storage account name and key, or use environment variables or Azure AD authentication for more secure credential management.

Using Azure SDKs (Example: Python)

Here's a Python example demonstrating how to delete a blob:

Python
from azure.storage.blob import BlobServiceClient

connection_string = "YOUR_CONNECTION_STRING"
container_name = "my-container"
blob_name = "my-blob.txt"

blob_service_client = BlobServiceClient.from_connection_string(connection_string)
blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name)

try:
    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}")

Remember to install the Azure Blob Storage SDK for Python: pip install azure-storage-blob.

Deleting a Blob Prefix

Deleting a blob prefix is a powerful way to remove multiple blobs that share a common path or naming convention. This is analogous to deleting a folder and its contents in a file system.

Using Azure CLI

To delete all blobs with the prefix logs/old/ from the my-container container:

Azure CLI
az storage blob delete-batch --account-name mystorageaccount --account-key YOUR_ACCOUNT_KEY --container my-container --prefix logs/old/

The --prefix parameter is crucial here.

Using Azure SDKs (Example: .NET)

While there isn't a direct delete_blob_prefix method in SDKs, you can achieve this by listing blobs with a prefix and then deleting them individually or in batches.

For very large numbers of blobs, consider using Azure Blob Storage Batch Operations for efficiency.

Managing Blob Deletions

Soft Delete

Azure Blob Storage supports soft delete for blobs, which protects your data from accidental deletions. When soft delete is enabled, deleted blobs are retained for a specified period, allowing you to recover them if needed.

It is highly recommended to enable soft delete for your containers to prevent data loss.

Lease Management

When attempting to delete a blob that has an active lease, the delete operation will fail unless the lease is released or broken. You must manage leases appropriately before deleting leased blobs.

Best Practices

Once a blob is permanently deleted (and soft delete retention period has passed), it cannot be recovered.

Next Steps

Explore related topics: