Azure Storage Documentation

Delete Blobs in Azure Storage

This document provides guidance on how to delete blobs from Azure Blob Storage using various methods.

Important: Deleting a blob is an irreversible operation. Ensure you have backed up any critical data before proceeding with deletion.

Methods for Deleting Blobs

You can delete blobs programmatically using Azure SDKs, the Azure CLI, Azure PowerShell, or through the Azure portal.

1. Using the Azure Portal

The Azure portal offers a user-friendly interface for managing your storage account and its contents.

  1. Navigate to your storage account in the Azure portal.
  2. Select "Containers" under "Data storage".
  3. Choose the container that holds the blob you wish to delete.
  4. Locate the blob in the list.
  5. Select the blob by clicking the checkbox next to its name.
  6. Click the "Delete" button that appears at the top of the list.
  7. Confirm the deletion in the dialog box.

2. Using Azure CLI

The Azure Command-Line Interface (CLI) is a powerful tool for automating Azure resource management.

To delete a single blob:

az storage blob delete --account-name  --container-name  --name  --account-key 

To delete multiple blobs using a prefix:

az storage blob delete-batch --account-name  --destination / --pattern "*" --account-key 

Replace the placeholders with your actual storage account name, container name, blob name, prefix, and account key.

3. Using Azure PowerShell

Azure PowerShell provides another scripting option for managing Azure resources.

To delete a single blob:

Remove-AzStorageBlob -Container  -Blob  -Context (New-AzStorageContext -StorageAccountName "" -StorageAccountKey "")

To delete multiple blobs with a prefix:

Get-AzStorageBlob -Container  -Prefix "" | Remove-AzStorageBlob -Context (New-AzStorageContext -StorageAccountName "" -StorageAccountKey "")

Remember to substitute the placeholder values.

4. Using Azure SDKs (Example: .NET)

You can integrate blob deletion into your applications using the Azure Storage SDKs.

Here's a C# example using the Azure.Storage.Blobs NuGet package:

C# Example

using Azure.Storage.Blobs;
using Azure;

// Replace with your connection string
string connectionString = "YOUR_AZURE_STORAGE_CONNECTION_STRING";
string containerName = "my-container";
string blobName = "my-blob.txt";

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

    // Get a BlobContainerClient
    BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);

    // Get a BlobClient
    BlobClient blobClient = containerClient.GetBlobClient(blobName);

    // Delete the blob
    Response response = await blobClient.DeleteAsync();

    Console.WriteLine($"Blob '{blobName}' deleted successfully.");
}
catch (RequestFailedException ex)
{
    Console.WriteLine($"Error deleting blob: {ex.Message}");
}
        

Warning: Using the blob name directly might be inefficient for large numbers of blobs. Consider using blob prefixes or batch operations for bulk deletion.

Deleting Previous Blob Versions

If blob versioning is enabled for your container, deleting a blob does not permanently remove it. Instead, it marks the current version as the latest. To permanently delete previous versions:

Best Practices for Deletion

Tip: Regularly review your storage usage and delete unneeded blobs and their versions to manage costs effectively.

Further Reading