All Products
Search
Document Center

Object Storage Service:Delete a bucket (Go SDK V2)

Last Updated:Aug 05, 2025

If you no longer need a bucket, you can delete it to prevent unnecessary charges.

Notes

  • The sample code in this topic uses the region IDcn-hangzhou for the China (Hangzhou) region. By default, the public endpoint is used to access resources in a bucket. If you want to access resources in the bucket using other Alibaba Cloud services within the same region, use the internal endpoint. For more information about OSS regions and endpoints, see OSS regions and endpoints.

  • In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials.

  • The name of a bucket must be unique. After a bucket is deleted, its name becomes available for other users to use. If you want to continue to use the name, we recommend that you empty the bucket instead of deleting it.

  • After a bucket is deleted, its data is permanently deleted and cannot be restored. Before you delete a bucket, make sure that you no longer need the data it contains. If you want to continue to use the data, back it up in advance. For more information about how to back up data, see Back up a bucket.

Permissions

By default, an Alibaba Cloud account has full permissions. RAM users or RAM roles under an Alibaba Cloud account do not have any permissions by default. The Alibaba Cloud account or account administrator must grant operation permissions through RAM Policy or Bucket Policy.

API

Action

Definition

DeleteBucket

oss:DeleteBucket

Deletes a bucket.

Note

If you have the oss:DeleteBucket permission in your RAM policy but still cannot delete the bucket, the bucket policy may contain an oss:DeleteBucket permission with an effect of Deny. You must change Deny to Allow or delete the bucket policy before you can delete the bucket.

Prerequisites

  • All objects in the bucket are deleted.

    Important

    If versioning is enabled for the bucket, make sure that all current and previous versions of objects in the bucket are deleted. For more information, see Versioning.

    • If the bucket contains a small number of objects, you can manually delete them. For more information, see Delete objects.

    • If the bucket contains many objects, you can configure a lifecycle rule to automatically delete the objects. For more information, see Lifecycle.

  • All parts that are generated from multipart or resumable uploads in the bucket are deleted. For more information, see Delete parts.

Method definition

func (c *Client) DeleteBucket(ctx context.Context, request *DeleteBucketRequest, optFns ...func(*Options)) (*DeleteBucketResult, error)

Request parameters

Parameter

Type

Description

ctx

context.Context

The context of the request. You can use this parameter to set the total time limit for the request.

request

*DeleteBucketRequest

The request parameters for the API operation. For more information, see DeleteBucketRequest.

optFns

...func(*Options)

Optional. The operation-level configuration parameters. For more information, see Options.

Response parameters

Return value

Type

Description

result

*DeleteBucketResult

The return value of the API operation. This parameter is valid only if the value of err is nil. For more information, see DeleteBucketResult.

err

error

The status of the request. If the request fails, the value of err is not nil.

Sample code

You can use the following code to delete a bucket.

package main

import (
	"context"
	"flag"
	"log"

	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)

var (
	region     string
	bucketName string
)

func init() {
	// Define command line parameters to specify the region and bucket name.
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.")
	flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
}

func main() {
	// Parse command line parameters.
	flag.Parse()

	// Check whether the bucket name parameter is empty.
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	// Check whether the region parameter is empty.
	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

	// Load the default configurations and set the credential provider and region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	// Create an OSS client.
	client := oss.NewClient(cfg)

	// Create a request object to delete the bucket.
	request := &oss.DeleteBucketRequest{
		Bucket: oss.Ptr(bucketName),
	}

	// Call the DeleteBucket method to delete the bucket.
	result, err := client.DeleteBucket(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to delete bucket %v", err)
	}

	// Print the deletion result.
	log.Printf("delete bucket result:%#v\n", result)
}

References

  • For the complete sample code to delete a bucket, see GitHub example.

  • For more information about the API operation used to delete a bucket, see DeleteBucket.