You can delete a bucket when you no longer need it or want to stop billing for it. OSS charges are mainly for resources within a bucket. Before you can delete a bucket, you must clear all 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 deletion. The bucket name also becomes available for other users to register. To completely stop using the OSS service, you must delete all buckets under your account.
After a bucket is deleted, its name is released and can be used by other users. To keep the name, empty the bucket's contents instead of deleting the bucket.
Data in a deleted bucket cannot be recovered. Before deleting the bucket, confirm that the data is no longer needed. If you still need the data, back it up first. For more information about backups, 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 using OSS SDK for Python 1.0.
In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Initialization.
Before you delete a bucket, make sure to clear the required resources.
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 policies.
API | Action | Definition |
DeleteBucket |
| Deletes a bucket. |
Sample Code
The following sample code shows how to delete a bucket named examplebucket:
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from environment variables. Before you run this code sample, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the Endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the region that corresponds to the Endpoint, for example, cn-hangzhou. Note that this parameter is required for V4 signatures.
region = "cn-hangzhou"
# Set yourBucketName to the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)
try:
# Delete the bucket.
bucket.delete_bucket()
except oss2.exceptions.BucketNotEmpty:
print('bucket is not empty.')
except oss2.exceptions.NoSuchBucket:
print('bucket does not exist')
References
For a complete code example of how to delete a bucket, see the GitHub example.
For more information about the API operation that you can call to delete a bucket, see DeleteBucket.