Azure Storage Documentation

Learn how to build, deploy, and manage your applications on Azure

Delete Blobs in Azure Storage

This document explains how to delete blobs, blob snapshots, and blob versions from Azure Blob Storage.

Important: Blob deletion is a permanent operation. Once a blob is deleted, it cannot be recovered unless you have versioning or soft delete enabled.

Understanding Deletion Options

Azure Blob Storage offers several ways to manage blob deletion:

Prerequisites

Methods for Deleting Blobs

You can delete blobs using the Azure portal, Azure CLI, Azure PowerShell, or Azure Storage client libraries (SDKs).

1. Using the Azure Portal

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

  1. Navigate to your storage account in the Azure portal.
  2. Select Containers under Data storage.
  3. Select the container that holds the blob you want to delete.
  4. Locate the blob in the list.
  5. Select the checkbox next to the blob name.
  6. Click the Delete button that appears at the top of the container blade.
  7. Confirm the deletion in the dialog box.
You can select multiple blobs for deletion by checking their respective checkboxes.

2. Using Azure CLI

The Azure Command-Line Interface (CLI) is a powerful tool for scripting storage operations.

To delete a blob:

Bash
az storage blob delete \
    --account-name <storage-account-name> \
    --container-name <container-name> \
    --name <blob-name> \
    --account-key <storage-account-key>

Replace the placeholders with your actual storage account name, container name, blob name, and account key. You can also use SAS tokens for authentication.

To delete a blob snapshot:

Bash
az storage blob delete \
    --account-name <storage-account-name> \
    --container-name <container-name> \
    --name <blob-name> \
    --snapshot <snapshot-time> \
    --account-key <storage-account-key>

To delete all snapshots of a blob:

Bash
az storage blob delete \
    --account-name <storage-account-name> \
    --container-name <container-name> \
    --name <blob-name> \
    --delete-snapshots include \
    --account-key <storage-account-key>

3. Using Azure PowerShell

Azure PowerShell provides cmdlets for managing Azure resources.

To delete a blob:

PowerShell
Remove-AzStorageBlob `
    -AccountName <storage-account-name> `
    -Container <container-name> `
    -Blob <blob-name> `
    -Force

To delete all snapshots of a blob:

PowerShell
Remove-AzStorageBlob `
    -AccountName <storage-account-name> `
    -Container <container-name> `
    -Blob <blob-name> `
    -IncludeSnapshots `
    -Force

4. Using Azure Storage Client Libraries (SDKs)

You can programmatically delete blobs using the Azure Storage SDKs available for various programming languages.

Example using .NET:

C#
using Azure.Storage.Blobs;

// Replace with your actual connection string
string connectionString = "YOUR_AZURE_STORAGE_CONNECTION_STRING";
string containerName = "mycontainer";
string blobName = "myblob.txt";

BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
BlobClient blobClient = containerClient.GetBlobClient(blobName);

try
{
    Response response = await blobClient.DeleteAsync();
    if (response.Value)
    {
        Console.WriteLine($"Blob '{blobName}' deleted successfully.");
    }
    else
    {
        Console.WriteLine($"Failed to delete blob '{blobName}'.");
    }
}
catch (RequestFailedException e)
{
    Console.WriteLine($"Error deleting blob: {e.Message}");
}

Configuring Deletion Protection and Soft Delete

To protect against accidental deletion, you can configure:

These features can be configured at the storage account level and provide a critical safety net for your data.

Enabling Soft Delete

Soft delete can be enabled for blobs, blob snapshots, and blob versions. You can configure the retention period from 1 to 365 days.

Enabling Blob Versioning

When versioning is enabled, every overwrite or delete operation on a blob creates a new version. You can then restore previous versions.

Enabling soft delete or versioning incurs additional storage costs. Ensure you understand these costs before enabling them.

Troubleshooting Deletion Issues

Further Reading