Azure Logo Azure Storage Documentation

Delete Blobs

This document provides comprehensive guidance on deleting blobs from Azure Blob Storage. You can delete individual blobs, snapshots, or entire blob types.

Note: Deleted blobs are permanently removed and cannot be recovered unless you have configured soft delete.

Prerequisites

Methods for Deleting Blobs

1. Using the Azure Portal

The Azure portal offers a user-friendly interface for managing your blobs.

  1. Navigate to your storage account in the Azure portal.
  2. Select Containers under the Data storage section.
  3. Click on the container that holds the blob you wish to delete.
  4. Locate the blob in the list.
  5. Click the ellipsis (...) next to the blob name and select Delete.
  6. Confirm the deletion when prompted.

2. Using Azure CLI

The Azure Command-Line Interface (CLI) is a powerful tool for automating storage management tasks.


az storage blob delete --account-name <your-storage-account-name> --container-name <your-container-name> --name <your-blob-name>
            

To delete multiple blobs, you can use a loop or the az storage blob delete-batch command.

3. Using Azure PowerShell

Azure PowerShell provides cmdlets for managing Azure resources.


Remove-AzStorageBlob -Container <your-container-name> -Blob <your-blob-name> -Context <storage-account-context>
            

4. Using SDKs (e.g., .NET, Python, Java)

Azure Storage SDKs allow you to programmatically delete blobs within your applications.

Example: Python SDK

from azure.storage.blob import BlobServiceClient

connection_string = "YOUR_AZURE_STORAGE_CONNECTION_STRING"
blob_service_client = BlobServiceClient.from_connection_string(connection_string)

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

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.")
except Exception as ex:
    print(f"Error deleting blob: {ex}")
            

Deleting Blob Snapshots

When you delete a blob that has snapshots, by default, only the active blob is deleted. The snapshots remain. To delete a specific snapshot, you need to provide its snapshot ID.


az storage blob delete --account-name <your-storage-account-name> --container-name <your-container-name> --name <your-blob-name> --snapshot <snapshot-id>
            

To delete all snapshots along with the blob, you can use the --delete-snapshots all option in Azure CLI or the equivalent in other tools.

Important: Deleting all snapshots will permanently remove all versions of the blob.

Deleting an Entire Blob Type

If you need to delete all blobs of a certain type (e.g., all .jpg files), you can iterate through them using the appropriate SDK or CLI commands and delete them individually or in batches.

Soft Delete for Blobs

Soft delete protects your data by retaining deleted blobs and snapshots for a specified period. During this period, you can recover deleted data.

To enable soft delete:

When soft delete is enabled, calling a delete operation moves the blob to a soft-deleted state. You can then undelete it within the retention period.

Tip: It's highly recommended to enable soft delete for your production storage accounts to prevent accidental data loss.

Best Practices