When you no longer need a bucket, delete it to stop billing. Object Storage Service (OSS) charges are primarily for resources within a bucket. Before you can delete a bucket, you must clear all of its resources. Deleting the bucket is the most reliable way to ensure that you do not miss any billable resources and incur unexpected fees. Note that data cannot be recovered after it is deleted. The bucket name also becomes available for other users to register. To stop using the OSS service completely, you must delete all buckets under your account.
After a bucket is deleted, its name is released and can be claimed by another user. To reserve the bucket name, empty the bucket instead of deleting it.
Bucket deletion is permanent and data cannot be recovered. Before proceeding, confirm the data is no longer needed or back it up first. For instructions, see Back up a bucket.
Usage notes
In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see 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.
This topic demonstrates creating an OSSClient instance with an OSS endpoint. For alternative configurations, such as using a custom domain or authenticating with credentials from Security Token Service (STS), see Configure OSSClient instances.
Before deleting the bucket, clean up resources that must be deleted.
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 policies.
API | Action | Definition |
DeleteBucket |
| Deletes a bucket. |
API | Action | Definition |
DeleteBucket |
| Deletes a bucket. |
Sample code
The following sample code shows how to delete a bucket named examplebucket:
package main
import (
"fmt"
"os"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
/// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint.
// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. Specify the actual region.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Specify the version of the signature algorithm.
clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
client, err := oss.New("yourEndpoint", "", "", clientOptions...)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Specify the name of the bucket. Example: examplebucket.
// Delete the bucket.
err = client.DeleteBucket("examplebucket")
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
}
References
For the complete sample code that is used to delete a bucket, visit GitHub.
For more information about the API operation that you can call to delete a bucket, see DeleteBucket.