This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can trysigning in orchanging directories.
Access to this page requires authorization. You can trychanging directories.
This article shows how to delete blobs using theAzure Storage client module for Go, and how to restoresoft-deleted blobs during the retention period.
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, addingimport paths, and creating an authorized client object. For details, seeGet started with Azure Blob Storage and Go.
Install theazblob module using the following command:
go get github.com/Azure/azure-sdk-for-go/sdk/storage/azblobTo authenticate with Microsoft Entra ID (recommended), install theazidentity module using the following command:
go get github.com/Azure/azure-sdk-for-go/sdk/azidentityIn your code file, add the following import paths:
import ( "github.com/Azure/azure-sdk-for-go/sdk/azidentity""github.com/Azure/azure-sdk-for-go/sdk/storage/azblob")These import paths represent the minimum needed to get started. Some code examples in this article might require additional import paths. For specific details and example usage, seeCode samples.
To connect an app to Blob Storage, create a client object usingazblob.NewClient. The following example shows how to create a client object usingDefaultAzureCredential for authorization:
func getServiceClientTokenCredential(accountURL string) *azblob.Client { // Create a new service client with token credential credential, err := azidentity.NewDefaultAzureCredential(nil) handleError(err) client, err := azblob.NewClient(accountURL, credential, nil) handleError(err) 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 roleStorage Blob Data Contributor or higher. To learn more, see the authorization guidance forDelete Blob (REST API) andUndelete Blob (REST API).
Note
When blob soft delete is enabled for a storage account, you can't perform a permanent deletion using client library methods. Using the methods in this article, a soft-deleted blob, blob version, or snapshot remains available until the retention period expires, at which time it's permanently deleted. To learn more about the underlying REST API operation, seeDelete Blob (REST API).
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) handleError(err)}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), }) handleError(err)}To deleteonly the snapshots and not the blob itself, you can pass the valueDeleteSnapshotsOptionTypeOnly to theDeleteSnapshots parameter.
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, seeSoft 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, seeBlob versioning. See one of the following sections, depending on your scenario:
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{}) handleError(err)}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) handleError(err) // Restore the blob version by copying it to the base blob _, err = baseBlobClient.StartCopyFromURL(context.TODO(), blobVersionClient.URL(), nil) handleError(err)}Note
The code samples in this guide are intended to help you get started with Azure Blob Storage and Go. You should modify error handling andContext 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.
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:
Was this page helpful?
Need help with this topic?
Want to try using Ask Learn to clarify or guide you through this topic?
Was this page helpful?
Want to try using Ask Learn to clarify or guide you through this topic?