Уреди

Делите путем


Delete and restore a blob container with Go

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.

Prerequisites

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 github.com/Azure/azure-sdk-for-go/sdk/storage/azblob

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

go get github.com/Azure/azure-sdk-for-go/sdk/azidentity

Add import paths

In 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, 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)
    handleError(err)

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

    return client
}

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:

func deleteContainer(client *azblob.Client, containerName string) {
    // Delete the container
    _, err := client.DeleteContainer(context.TODO(), containerName, nil)
    handleError(err)
}

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:

func restoreDeletedContainer(client *azblob.Client, containerName string) {
    // List containers, included deleted ones
    pager := client.NewListContainersPager(&azblob.ListContainersOptions{
        Include: azblob.ListContainersInclude{Deleted: true},
    })

    for pager.More() {
        resp, err := pager.NextPage(context.TODO())
        handleError(err)

        for _, container := range resp.ContainerItems {
            if *container.Name == containerName && *container.Deleted {
                // Restore the deleted container
                _, err := client.ServiceClient().RestoreContainer(context.TODO(), containerName, *container.Version, 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 and Context values to meet the needs of your application.

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:

Client module resources

See also

  • This article is part of the Blob Storage developer guide for Go. To learn more, see the full list of developer guide articles at Build your Go app.