Deleting Blobs
This document explains how to delete blobs in Azure Blob Storage. Deleting a blob is a permanent operation and cannot be undone.
Methods for Deleting Blobs
You can delete blobs using several methods:
- Azure portal
- Azure CLI
- Azure PowerShell
- Azure Storage SDKs (for various programming languages)
- REST API
Deleting a Single Blob
Using Azure Portal
1. Navigate to your storage account in the Azure portal.
2. Select your container.
3. Locate the blob you wish to delete.
4. Select the blob and click the Delete button in the toolbar.
5. Confirm the deletion when prompted.
Using Azure CLI
To delete a blob using the Azure CLI, use the az storage blob delete command.
az storage blob delete --account-name --container-name --name --auth-mode login
Replace the placeholders with your actual storage account name, container name, and blob name.
Using Azure PowerShell
To delete a blob using Azure PowerShell, use the Remove-AzStorageBlob cmdlet.
Remove-AzStorageBlob -Container -Blob -Context (Get-AzStorageAccount -ResourceGroupName -Name ).Context
Remember to replace the placeholder values.
Using .NET SDK (Example)
Here's a basic example of how to delete a blob using the Azure Storage .NET SDK:
using Azure.Storage.Blobs;
// Replace with your connection string, container name, and blob name
string connectionString = "YOUR_AZURE_STORAGE_CONNECTION_STRING";
string containerName = "my-container";
string blobName = "my-blob.txt";
BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
Response response = await containerClient.DeleteBlobAsync(blobName);
if (response.Value)
{
Console.WriteLine($"Blob '{blobName}' deleted successfully.");
}
else
{
Console.WriteLine($"Failed to delete blob '{blobName}'.");
}
Deleting Multiple Blobs
You can delete multiple blobs at once using blob prefixes or by iterating through them.
Deleting Blobs with a Prefix (Azure CLI)
To delete all blobs that start with a specific prefix:
az storage blob list --account-name --container-name --prefix "logs/" --output table --query "[].name" | ForEach-Object { az storage blob delete --account-name --container-name --name $_ --auth-mode login }
This command first lists blobs with the prefix "logs/" and then iterates through the results to delete each one.
Deleting All Blobs in a Container
Warning: This operation is irreversible and will delete all data within the specified container.
You can delete all blobs in a container by deleting the container itself, or by iterating through all blobs and deleting them individually.
Important: Blob deletion is a destructive operation. Always ensure you have backups or are certain before deleting data.
Soft Delete
Azure Blob Storage offers a soft delete feature for blobs and containers. When soft delete is enabled, deleted blobs and containers are retained for a specified period, allowing you to recover them if they were deleted accidentally.
You can configure soft delete settings at the storage account level. For more details, refer to the documentation on Blob Soft Delete.
Note: If soft delete is enabled, deleting a blob marks it as soft-deleted. The blob is then only permanently removed after the retention period expires or is explicitly purged.
Blob Snapshots and Deletions
Deleting a blob does not automatically delete its snapshots. To delete a blob and all its snapshots, you typically need to delete them individually or use specific options provided by the SDKs or REST API.
If a blob has snapshots, deleting the blob itself will not remove the snapshots. You must explicitly delete each snapshot.
Tip: Consider using a consistent naming convention for your blobs and their associated snapshots to simplify management and deletion.