All Products
Search
Document Center

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

Last Updated:Feb 27, 2026

Delete a bucket that you no longer need to avoid unnecessary charges.

Warning

Deleting a bucket permanently removes all its data. This action cannot be undone. Back up any data you still need before proceeding. For more information, see Back up a bucket.

Note

Bucket names are globally unique. After you delete a bucket, other users can claim its name. To keep the name, empty the bucket instead of deleting it.

Prerequisites

Before you begin, make sure that:

  • All objects in the bucket are deleted. If versioning is enabled, delete all current and previous versions.

    • For a small number of objects, delete them manually. For more information, see Delete objects.

    • For a large number of objects, configure a lifecycle rule to delete them automatically. For more information, see Lifecycle.

    • For more information about versioning, see Versioning.

  • All parts from incomplete multipart uploads or resumable uploads are deleted. For more information, see Delete parts.

  • Access credentials are configured as environment variables. For more information, see Configure access credentials.

Required permissions

By default, an Alibaba Cloud account has full permissions. RAM users and RAM roles do not have any permissions by default and must be granted access through a RAM policy or bucket policy.

APIActionDescription
DeleteBucketoss:DeleteBucketDeletes a bucket.
Note

If your RAM policy grants oss:DeleteBucket but the operation still fails, check whether the bucket policy contains a Deny rule for oss:DeleteBucket. Change the effect to Allow or remove the bucket policy.

Sample code

The following code deletes a bucket. The sample uses region ID cn-hangzhou and accesses the bucket through the public endpoint. To use the internal endpoint for access from other Alibaba Cloud services in the same region, see OSS regions and endpoints.

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() {
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.")
	flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
}

func main() {
	flag.Parse()

	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

	// Load the default configuration with environment variable credentials.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	client := oss.NewClient(cfg)

	// Send the delete bucket request.
	result, err := client.DeleteBucket(context.TODO(), &oss.DeleteBucketRequest{
		Bucket: oss.Ptr(bucketName),
	})
	if err != nil {
		log.Fatalf("failed to delete bucket: %v", err)
	}

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

Method definition

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

Request parameters

ParameterTypeDescription
ctxcontext.ContextThe request context. Use this to set a total time limit for the request.
request*DeleteBucketRequestThe request parameters for the DeleteBucket operation. For more information, see DeleteBucketRequest.
optFns...func(*Options)Optional. Operation-level configuration parameters. For more information, see Options.

Response parameters

ParameterTypeDescription
result*DeleteBucketResultThe response of the operation. Valid only when err is nil. For more information, see DeleteBucketResult.
errerrorThe error status of the request. Non-nil if the request fails.

References