If you no longer use a bucket, delete the bucket to stop unexpected charges.
- All objects in the bucket are deleted.
- For more information about how to manually delete a small number of objects, see Delete objects.
- To delete a large number of objects, we recommend that you configure lifecycle rules to batch delete the objects. For more information about how to delete a large number of objects, see Configure lifecycle rules.
If versioning is enabled for the bucket that you want to delete, make sure that all versions of objects in the bucket are deleted. For more information about how to delete all versions of objects in a bucket, see Configure versioning.
- Parts that are uploaded by multipart upload or resumable upload tasks in the bucket are deleted. For more information about how to delete parts in a bucket, see Manage parts.
- All LiveChannels in the bucket are deleted. For more information about how to delete LiveChannels, see DeleteLiveChannel.
The following code provides an example on how to delete a bucket named examplebucket:
# -*- coding: utf-8 -*- import oss2 # The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to access OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine Q&M. To create a RAM user, log on to the RAM console. auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret') # Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. # Specify the name of the bucket. Example: examplebucket. bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket') 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')