Delete a Blob
This document explains how to delete a blob from Azure Blob Storage using various methods, including the Azure portal, Azure CLI, Azure PowerShell, and client libraries.
Prerequisites
- An Azure Storage account.
- A container within the storage account.
- A blob within the container that you wish to delete.
Understanding Blob Deletion
When you delete a blob, it is permanently removed from the storage account. Azure Storage offers soft delete capabilities for blobs, which can protect against accidental deletions. If soft delete is enabled for blobs, deleting a blob will mark it as deleted but it will still be recoverable for a specified retention period. This section focuses on the permanent deletion of a blob.
Methods for Deleting Blobs
1. Using the Azure Portal
The Azure portal provides a user-friendly graphical interface for managing your storage resources.
- Navigate to your Azure Storage account in the Azure portal.
- Under the "Data storage" section, select "Containers".
- Click on the container that holds the blob you want to delete.
- Locate the blob in the list. You can use the search bar if needed.
- Select the checkbox next to the blob you wish to delete.
- Click the "Delete" button that appears at the top of the container view.
- Confirm the deletion in the dialog box.
2. Using Azure CLI
The Azure Command-Line Interface (CLI) is a powerful tool for managing Azure resources from your terminal.
To delete a blob:
az storage blob delete --account-name --container-name --name
Replace the placeholders with your actual storage account name, container name, and blob name.
To delete multiple blobs (e.g., all blobs with a specific prefix):
az storage blob delete-batch --account-name --container-name --pattern "prefix/*"
3. Using Azure PowerShell
Azure PowerShell provides cmdlets for managing Azure resources.
To delete a blob:
Remove-AzStorageBlob -Container -Blob -Context (New-AzStorageContext -StorageAccountName "" -StorageAccountKey "")
Ensure you have your storage account key or a shared access signature (SAS) token for authentication.
4. Using Client Libraries
Azure provides SDKs for various programming languages. Here's an example using the Python SDK:
from azure.storage.blob import BlobServiceClient
# Replace with your actual connection string
connect_str = "YOUR_AZURE_STORAGE_CONNECTION_STRING"
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
container_name = "your-container-name"
blob_name = "your-blob-name.txt"
try:
container_client = blob_service_client.get_container_client(container_name)
container_client.delete_blob(blob_name)
print(f"Blob '{blob_name}' deleted successfully from container '{container_name}'.")
except Exception as ex:
print(f"Error deleting blob: {ex}")
Similar methods are available in other SDKs (e.g., .NET, Java, Node.js).
Soft Delete and Undelete
If soft delete for blobs is enabled for your storage account, you can recover accidentally deleted blobs within the configured retention period. This is a crucial safety feature.
To undelete a blob using Azure CLI (if soft delete is enabled):
az storage blob undelete --account-name --container-name --blob-name --snapshot
Note that undeleting a blob often requires specifying a snapshot ID if the blob was versioned or snapshot.
Next Steps
- Learn how to copy blobs.
- Explore blob access tiers for cost optimization.