Delete buckets using OSS SDK for Go 2.0
Delete an Object Storage Service (OSS) bucket that you no longer need to avoid unnecessary charges.
Notes
-
The sample code in this topic uses the region ID
cn-hangzhouof the China (Hangzhou) region and the public endpoint by default. To access resources in the bucket from other Alibaba Cloud services in the same region, use the internal endpoint. For more information about OSS regions and endpoints, see OSS regions and endpoints. -
Access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials.
-
Bucket names must be unique. After a bucket is deleted, its name becomes available for others to use. If you want to keep the name, empty the bucket instead of deleting it.
-
Deleted buckets cannot be restored. Before you delete a bucket, make sure the data is no longer needed, or back it up in advance. For more information, see Back up buckets.
-
The
oss:DeleteBucketpermission is required for bucket deletion. For more information, see Grant a custom policy.
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 policies or Bucket Policy.
|
API |
Action |
Description |
|
DeleteBucket |
|
Deletes a bucket. |
If a RAM user has the oss:DeleteBucket permission in a RAM policy but cannot delete the bucket, a bucket policy attached to the RAM user may contain oss:DeleteBucket with the effect set to Deny. Change Deny to Allow or delete the bucket policy to resolve the issue.
Prerequisites
-
All objects in the bucket are deleted.
ImportantTo delete a versioned 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, we recommend that you manually delete them. For information, see Delete objects.
-
If the bucket contains a large number of objects, we recommend that you configure lifecycle rules to delete the objects. For information, see Lifecycle.
-
-
Parts generated by multipart upload or resumable upload tasks in the bucket are deleted. For more information, see Delete parts.
Method
func (c *Client) DeleteBucket(ctx context.Context, request *DeleteBucketRequest, optFns ...func(*Options)) (*DeleteBucketResult, error)
Request parameters
|
Parameter |
Type |
Description |
|
ctx |
context.Context |
The request context, used to specify the total duration of the request. |
|
request |
*DeleteBucketRequest |
The parameters of a specific API operation. For information, see DeleteBucketRequest. |
|
optFns |
...func(*Options) |
Optional. The operation-level parameter. For more information, see Options. |
Response parameters
|
Parameter |
Type |
Description |
|
result |
*DeleteBucketResult |
The response to the operation. Valid when err is nil. For more information, visit DeleteBucketResult. |
|
err |
error |
The error status. A non-nil value indicates a failed request. |
Sample code
The following sample code deletes 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 name of the bucket.
flag.StringVar(®ion, "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 name of the bucket is specified.
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
// Check whether the region is specified.
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
// Load the default configurations and specify 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 for deletion.
request := &oss.DeleteBucketRequest{
Bucket: oss.Ptr(bucketName),
}
// Call the DeleteBucket method.
result, err := client.DeleteBucket(context.TODO(), request)
if err != nil {
log.Fatalf("failed to delete bucket %v", err)
}
// Display the result.
log.Printf("delete bucket result:%#v\n", result)
}
References
-
For complete sample code for deleting a bucket, visit GitHub sample.
-
For the API reference for deleting a bucket, see DeleteBucket.