Delete and restore a blob with Go

This article shows how to delete blobs using the Azure Storage client module for Go. If you've enabled soft delete for blobs, you can restore deleted blobs during the retention period.


Set up your environment

If you don't have an existing project, this section shows how to set up a project to work with the Azure Blob Storage client module for Go. The steps include module installation, adding import paths, and creating an authorized client object. For details, see Get started with Azure Blob Storage and Go.

Install modules

Install the azblob module using the following command:

go get

To authenticate with Microsoft Entra ID (recommended), install the azidentity module using the following command:

go get

Add import paths

In your code file, add the following import paths:

import (

Some code examples in this article might require additional import paths. For specific details and example usage, see Code samples.

Create a client object

To connect an app to Blob Storage, create a client object using azblob.NewClient. The following example shows how to create a client object using DefaultAzureCredential for authorization:

func getServiceClientTokenCredential(accountURL string) *azblob.Client {
    // Create a new service client with token credential
    credential, err := azidentity.NewDefaultAzureCredential(nil)

    client, err := azblob.NewClient(accountURL, credential, nil)

    return client


The authorization mechanism must have the necessary permissions to delete a blob, or to restore a soft-deleted blob. For authorization with Microsoft Entra ID (recommended), you need Azure RBAC built-in role Storage Blob Data Contributor or higher. To learn more, see the authorization guidance for Delete Blob (REST API) and Undelete Blob (REST API).

Delete a blob

To delete a blob, call the following method:

The following example deletes a blob:

func deleteBlob(client *azblob.Client, containerName string, blobName string) {
    // Delete the blob
    _, err := client.DeleteBlob(context.TODO(), containerName, blobName, nil)

If the blob has any associated snapshots, you must delete all of its snapshots to delete the blob. The following example deletes a blob and its snapshots:

func deleteBlobWithSnapshots(client *azblob.Client, containerName string, blobName string) {
    // Delete the blob and its snapshots
    _, err := client.DeleteBlob(context.TODO(), containerName, blobName, &blob.DeleteOptions{
        DeleteSnapshots: to.Ptr(blob.DeleteSnapshotsOptionTypeInclude),

To delete only the snapshots and not the blob itself, you can pass the value DeleteSnapshotsOptionTypeOnly to the DeleteSnapshots parameter.

Restore a deleted blob

Blob soft delete protects an individual blob and its versions, snapshots, and metadata from accidental deletes or overwrites by maintaining the deleted data in the system for a specified period of time. During the retention period, you can restore the blob to its state at deletion. After the retention period expires, the blob is permanently deleted. For more information about blob soft delete, see Soft delete for blobs.

You can use the Azure Storage client libraries to restore a soft-deleted blob or snapshot.

How you restore a soft-deleted blob depends on whether or not your storage account has blob versioning enabled. For more information on blob versioning, see Blob versioning. See one of the following sections, depending on your scenario:

Restore soft-deleted objects when versioning is disabled

To restore deleted blobs when versioning is disabled, call the following method:

This method restores the content and metadata of a soft-deleted blob and any associated soft-deleted snapshots. Calling this method for a blob that hasn't been deleted has no effect.

func restoreDeletedBlob(client *azblob.Client, containerName string, blobName string) {
    // Reference the blob as a client object
    blobClient := client.ServiceClient().NewContainerClient(containerName).NewBlobClient(blobName)

    // Restore the deleted blob
    _, err := blobClient.Undelete(context.TODO(), &blob.UndeleteOptions{})

Restore soft-deleted objects when versioning is enabled

If a storage account is configured to enable blob versioning, deleting a blob causes the current version of the blob to become the previous version. To restore a soft-deleted blob when versioning is enabled, copy a previous version over the base blob. You can use the following method:

The following code example identifies a version of a deleted blob, and restores that version by copying it to the base blob:

func restoreDeletedBlobVersion(client *azblob.Client, containerName string, blobName string, versionID string) {
    // Reference the blob as a client object
    baseBlobClient := client.ServiceClient().NewContainerClient(containerName).NewBlobClient(blobName)

    blobVersionClient, err := baseBlobClient.WithVersionID(versionID)

    // Restore the blob version by copying it to the base blob
    _, err = baseBlobClient.StartCopyFromURL(context.TODO(), blobVersionClient.URL(), nil)


The code samples in this guide are intended to help you get started with Azure Blob Storage and Go. You should modify error handling and Context values to meet the needs of your application.


To learn more about how to delete blobs and restore deleted blobs using the Azure Blob Storage client module for Go, see the following resources.

Code samples

REST API operations

The Azure SDK for Go contains libraries that build on top of the Azure REST API, allowing you to interact with REST API operations through familiar Go paradigms. The client library methods for deleting blobs and restoring deleted blobs use the following REST API operations:

Client module resources

See also