Delete Blobs in Azure Storage

This document explains how to delete blobs from an Azure Storage container using various methods, including the Azure portal, Azure CLI, PowerShell, and SDKs.

Important Considerations

  • Once a blob is deleted, it is permanently removed and cannot be recovered unless soft delete is enabled for blobs.
  • Ensure you have the necessary permissions to delete blobs.
  • Deleting a large number of blobs can take time and incur costs based on operations.

Deleting Blobs via the Azure Portal

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

  1. Navigate to your storage account in the Azure portal.
  2. Under "Data storage", select "Containers".
  3. Click on the container that holds the blobs you want to delete.
  4. Select the blob(s) you wish to delete by checking the box next to their names. You can select multiple blobs or a whole folder.
  5. Click the "Delete" button that appears at the top of the blob list.
  6. Confirm the deletion in the dialog box.

Deleting Blobs using Azure CLI

The Azure Command-Line Interface (CLI) is a powerful tool for scripting and automating Azure tasks.

Delete a single blob

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

Delete multiple blobs using a prefix

This command deletes all blobs whose names start with a specified prefix.

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

Delete all blobs in a container

Use this with caution, as it will permanently remove all blobs.

az storage blob delete-batch --account-name  --container-name  --all
            

Deleting Blobs using Azure PowerShell

Azure PowerShell offers cmdlets for managing Azure resources.

Delete a single blob

Remove-AzStorageBlob -Container  -Blob  -Context 
            

You can get the storage account context using Get-AzStorageAccount -ResourceGroupName -Name .

Delete multiple blobs using a prefix

Get-AzStorageBlob -Container  -Prefix "prefix-" -Context  | Remove-AzStorageBlob -Force -Context 
            

Deleting Blobs using .NET SDK

Here's an example of how to delete blobs using the Azure Blob Storage client library for .NET.

Ensure you have the Azure.Storage.Blobs NuGet package installed.

using Azure.Storage.Blobs;
            using System;

            // Replace with your connection string and container name
            string connectionString = "YOUR_AZURE_STORAGE_CONNECTION_STRING";
            string containerName = "mycontainer";
            string blobNameToDelete = "myblob.txt";
            string blobPrefix = "folder/subfolder/";

            BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
            BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);

            // Delete a single blob
            try
            {
                Response response = await containerClient.DeleteBlobAsync(blobNameToDelete);
                if (response.Value)
                {
                    Console.WriteLine($"Blob '{blobNameToDelete}' deleted successfully.");
                }
            }
            catch (RequestFailedException ex)
            {
                Console.WriteLine($"Error deleting blob '{blobNameToDelete}': {ex.Message}");
            }

            // Delete blobs with a prefix
            try
            {
                await foreach (BlobItem blobItem in containerClient.GetBlobsAsync(prefix: blobPrefix))
                {
                    await containerClient.DeleteBlobAsync(blobItem.Name);
                    Console.WriteLine($"Blob '{blobItem.Name}' deleted.");
                }
            }
            catch (RequestFailedException ex)
            {
                Console.WriteLine($"Error deleting blobs with prefix '{blobPrefix}': {ex.Message}");
            }
            

Tip: Blob Soft Delete

To protect against accidental deletion, enable Soft delete for blobs on your storage account. This retains deleted blobs for a specified retention period, allowing you to undelete them. You can configure this in the Azure portal under your storage account's "Data protection" settings.

Refer to the official Azure documentation for more details and examples in other languages.

Official Azure Blob Deletion Documentation