title | titleSuffix | description | services | author | ms.service | ms.topic | ms.date | ms.author | ms.devlang | ms.custom |
---|---|---|---|---|---|---|---|---|---|---|
Delete and restore a blob container with Go | Azure Storage | Learn how to delete and restore a blob container in your Azure Storage account using the Go client library. | storage | pauljewellmsft | azure-blob-storage | how-to | 08/05/2024 | pauljewell | golang | devx-track-go, devguide-go |
[!INCLUDE storage-dev-guide-selector-delete-container]
This article shows how to delete containers with the Azure Storage client module for Go. If you've enabled container soft delete, you can restore deleted containers.
[!INCLUDE storage-dev-guide-prereqs-go]
[!INCLUDE storage-dev-guide-project-setup-go]
The authorization mechanism must have the necessary permissions to delete or restore a container. 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 Container (REST API) and Restore Container (REST API).
To delete a container, call the following method:
After you delete a container, you can't create a container with the same name for at least 30 seconds. Attempting to create a container with the same name fails with HTTP error code 409 (Conflict)
. Any other operations on the container or the blobs it contains fail with HTTP error code 404 (Not Found)
.
The following example shows how to delete a specified container:
:::code language="go" source="~/blob-devguide-go/cmd/delete-container/delete_container.go" id="snippet_delete_container":::
When container soft delete is enabled for a storage account, a deleted container and its contents can be recovered within a specified retention period. To learn more about container soft delete, see Enable and manage soft delete for containers. You can restore a soft-deleted container by calling the following method from the embedded ServiceClient for the client object:
The following example lists containers, including soft-deleted containers, and iterates over the list to restore the specified soft-deleted container:
:::code language="go" source="~/blob-devguide-go/cmd/delete-container/delete_container.go" id="snippet_restore_container":::
[!INCLUDE storage-dev-guide-code-samples-note-go]
To learn more about deleting a container 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 or restoring a container use the following REST API operations:
- Delete Container (REST API)
- Restore Container (REST API)
[!INCLUDE storage-dev-guide-resources-go]
[!INCLUDE storage-dev-guide-next-steps-go]