Delete a bucket (Python SDK V1)

Updated at:

If you no longer need a bucket, you can delete it to stop all billing. Because Object Storage Service (OSS) fees are charged for the resources inside a bucket, you must clear all resources before deleting it. Deleting a bucket is the most reliable way to prevent unexpected fees from overlooked billable resources. Note that data cannot be recovered after a bucket is deleted, and another user can claim its name after it is released. To completely stop using OSS, you must delete all buckets in your account.

Warning
  • After a bucket is deleted, its name is released and can be claimed by another user. If you want to keep the bucket name, empty the bucket instead of deleting it.

  • Data in a bucket cannot be recovered after the bucket is deleted. Before you delete a bucket, confirm that the data is no longer needed. If you want to continue using the data in the bucket, back it up in advance. For more information, see Back up a bucket.

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.

  • 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 Initialization.

  • Before you delete a bucket, make sure that you have deleted 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 policies or Bucket Policy.

API

Action

Description

DeleteBucket

oss:DeleteBucket

Deletes a bucket.

Sample Code

The following code provides an example of how to delete the 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.