Azure Storage Documentation

Deleting Objects (Blobs) from Azure Blob Storage

This document outlines the methods for deleting objects (blobs) from Azure Blob Storage using various tools and SDKs.

Important: Blob deletion is a permanent operation. Once a blob is deleted, it cannot be recovered unless soft delete is enabled for the container.

Prerequisites

Methods for Deleting Blobs

1. Using Azure CLI

The Azure Command-Line Interface (CLI) provides a straightforward way to manage your Azure resources, including deleting blobs.

Deleting a single blob


az storage blob delete \
    --account-name  \
    --container-name  \
    --name  \
    --account-key   # Or use --connection-string or Azure AD authentication
            

Replace placeholders like <your-storage-account-name>, <your-container-name>, <your-blob-name>, and <your-account-key> with your specific values.

Deleting multiple blobs (using a prefix)

You can delete all blobs that start with a specific prefix.


az storage blob delete-batch \
    --account-name  \
    --container-name  \
    --pattern "my-prefix-*" \
    --account-key 
            

The --pattern argument supports wildcard characters. For example, "my-prefix-*" will delete all blobs whose names start with "my-prefix-".

2. Using Azure PowerShell

Azure PowerShell offers cmdlets for managing blob storage.

Deleting a single blob


Remove-AzStorageBlob `
    -ConnectionString "" `
    -ContainerName "" `
    -Blob ""
            

Ensure you have the Azure PowerShell module installed and are logged in.

Deleting multiple blobs

You can use Get-AzStorageBlob with a filter and pipe it to Remove-AzStorageBlob.


Get-AzStorageBlob `
    -ConnectionString "" `
    -ContainerName "" `
    -Prefix "old-files/" `
    | Remove-AzStorageBlob
            

3. Using Azure SDKs (e.g., .NET, Python, Java, Node.js)

The Azure SDKs provide programmatic control for deleting blobs within your applications.

Example: Python SDK


from azure.storage.blob import BlobServiceClient

connect_str = ""
container_name = ""
blob_name = ""

# Create the BlobServiceClient object
blob_service_client = BlobServiceClient.from_connection_string(connect_str)

# Get a client to interact with a specific blob
blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name)

try:
    # Delete the blob
    blob_client.delete_blob()
    print(f"Blob '{blob_name}' deleted successfully from container '{container_name}'.")
except Exception as e:
    print(f"Error deleting blob: {e}")
            

Example: .NET SDK


using Azure.Storage.Blobs;
using System;

string connectionString = "";
string containerName = "";
string blobName = "";

// Create a BlobServiceClient object
BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);

// Get a client to interact with a specific blob
BlobClient blobClient = blobServiceClient.GetBlobClient(blobName);

try
{
    // Delete the blob
    var response = await blobClient.DeleteAsync();
    Console.WriteLine($"Blob '{blobName}' deleted successfully from container '{containerName}'.");
}
catch (Exception ex)
{
    Console.WriteLine($"Error deleting blob: {ex.Message}");
}
            

Refer to the official Azure SDK documentation for your preferred language for detailed examples and advanced options, such as conditional deletion or deleting snapshots.

Important Considerations