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.

Note: Deleting a blob is a permanent operation. Once a blob is deleted, it cannot be recovered unless versioning is enabled and you restore a previous version.

Methods for Deleting Blobs

1. Using the Azure Portal

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

  1. Navigate to your storage account in the Azure portal.
  2. Select "Containers" under "Data storage" in the left-hand menu.
  3. Click on the container that holds the blob you want to delete.
  4. Find the blob in the list. You can use the search bar to locate it quickly.
  5. Select the blob by clicking the checkbox next to its name.
  6. Click the "Delete" button at the top of the blob list.
  7. 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.");
        
Warning: When deleting a blob, consider its impact on applications or services that might be dependent on it. Implement appropriate error handling and logging for deletion operations.

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