Skip to content

Latest commit

 

History

History
76 lines (45 loc) · 4.34 KB

storage-blob-container-delete-go.md

File metadata and controls

76 lines (45 loc) · 4.34 KB
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
05/22/2024
pauljewell
golang
devx-track-go, devguide-go

Delete and restore a blob container with 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]

Set up your environment

[!INCLUDE storage-dev-guide-project-setup-go]

Authorization

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).

Delete a container

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":::

Restore a deleted 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]

Resources

To learn more about deleting a container 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 or restoring a container use the following REST API operations:

[!INCLUDE storage-dev-guide-resources-go]

See also