title | titleSuffix | description | services | author | ms.author | ms.date | ms.service | ms.topic | ms.devlang | ms.custom |
---|---|---|---|---|---|---|---|---|---|---|
Delete and restore a blob with Go | Azure Storage | Learn how to delete and restore a blob in your Azure Storage account using the Go client library. | storage | pauljewellmsft | pauljewell | 08/12/2024 | azure-blob-storage | how-to | golang | devx-track-go, devguide-go |
[!INCLUDE storage-dev-guide-selector-delete-blob]
This article shows how to delete blobs using the Azure Storage client module for Go, and how to restore soft-deleted blobs during the retention period.
[!INCLUDE storage-dev-guide-prereqs-go]
[!INCLUDE storage-dev-guide-project-setup-go]
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).
[!INCLUDE storage-dev-guide-delete-blob-note]
To delete a blob, call the following method:
The following example deletes a blob:
:::code language="go" source="~/blob-devguide-go/cmd/delete_blob/delete_blob.go" id="snippet_delete_blob":::
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:
:::code language="go" source="~/blob-devguide-go/cmd/delete_blob/delete_blob.go" id="snippet_delete_blob_snapshots":::
To delete only the snapshots and not the blob itself, you can pass the value DeleteSnapshotsOptionTypeOnly
to the DeleteSnapshots
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, 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:
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.
:::code language="go" source="~/blob-devguide-go/cmd/delete_blob/delete_blob.go" id="snippet_restore_blob":::
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:
:::code language="go" source="~/blob-devguide-go/cmd/delete_blob/delete_blob.go" id="snippet_restore_blob_version":::
[!INCLUDE storage-dev-guide-code-samples-note-go]
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.
- View code samples from this article (GitHub)
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:
- Delete Blob (REST API)
- Undelete Blob (REST API)
[!INCLUDE storage-dev-guide-resources-go]
[!INCLUDE storage-dev-guide-next-steps-go]