Delete Blobs in Azure Storage
This document explains how to delete blobs from your Azure Storage account using various methods, including the Azure portal, Azure CLI, Azure PowerShell, and the Azure Storage SDKs.
Methods for Deleting Blobs
1. Using the Azure Portal
The Azure portal provides a user-friendly interface for managing your storage blobs.
- Navigate to your storage account in the Azure portal.
- Select "Containers" under "Data storage" in the left-hand menu.
- Click on the container that holds the blob you want to delete.
- Find the blob in the list. You can use the search bar to locate it quickly.
- Select the blob by clicking the checkbox next to its name.
- Click the "Delete" button at the top of the blob list.
- Confirm the deletion when prompted.
2. Using Azure CLI
The Azure Command-Line Interface (CLI) is a powerful tool for automating storage operations.
To delete a single blob:
az storage blob delete --account-name --container-name --name --auth-mode login
To delete multiple blobs using a snapshot (e.g., delete all blobs in a container):
az storage blob delete-batch --account-name --destination --pattern "*" --auth-mode login
Replace placeholders like <YOUR_STORAGE_ACCOUNT_NAME>, <YOUR_CONTAINER_NAME>, and <YOUR_BLOB_NAME> with your actual values.
3. Using Azure PowerShell
Azure PowerShell offers cmdlets for managing Azure resources.
To delete a single blob:
Remove-AzStorageBlob -Container -Blob -Context (New-AzStorageContext -StorageAccountName "" -StorageAccountKey "")
To delete all blobs in a container:
Get-AzStorageBlob -Container -Context (New-AzStorageContext -StorageAccountName "" -StorageAccountKey "") | Remove-AzStorageBlob
Note: It's recommended to use Managed Identity or Service Principal for authentication instead of account keys in production environments.
4. Using Azure Storage SDKs
The Azure Storage SDKs allow you to programmatically delete blobs from your applications.
Example: Python SDK
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
connection_string = "YOUR_CONNECTION_STRING"
container_name = "mycontainer"
blob_name = "myblob.txt"
# Create the BlobServiceClient object
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
# Get a client to the blob
blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name)
# Delete the blob
print(f"Deleting blob: {blob_name}")
blob_client.delete_blob()
print(f"Blob {blob_name} deleted successfully.")
Example: .NET SDK
using Azure.Storage.Blobs;
using System;
string connectionString = "YOUR_CONNECTION_STRING";
string containerName = "mycontainer";
string blobName = "myblob.txt";
// Create a BlobServiceClient
BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
// Get a client to the container
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
// Delete the blob
Console.WriteLine($"Deleting blob: {blobName}");
Response response = containerClient.DeleteBlob(blobName);
Console.WriteLine($"Blob {blobName} deleted successfully.");
Deleting Blob Snapshots
When you delete a blob that has snapshots, only the current version of the blob is deleted. The snapshots remain. To delete a specific snapshot, you must specify its snapshot identifier.
Using Azure CLI to delete a snapshot:
az storage blob delete --account-name --container-name --name --snapshot <SNAPSHOT_ID> --auth-mode login
Using Azure CLI to delete all snapshots for a blob:
az storage blob delete --account-name --container-name --name --delete-snapshots true --auth-mode login
Replace <SNAPSHOT_ID> with the actual snapshot ID.
Important Considerations
- Versioning: If blob versioning is enabled on your storage account, deleting a blob creates a new version. You can then restore previous versions if needed.
- Immutability Policies: If your container has an immutability policy, you might not be able to delete blobs until the retention period expires.
- Soft Delete: Soft delete for blobs protects against accidental deletions. If enabled, deleted blobs are retained for a specified period and can be recovered.
- Leased Blobs: If a blob has an active lease, you must break the lease before you can delete it.