All Products
Search
Document Center

Object Storage Service:Delete a bucket (Python SDK V2)

Last Updated:Mar 19, 2026

Use the delete_bucket method in OSS Python SDK V2 to permanently delete a bucket.

Important

Deleting a bucket is irreversible. Back up any data you need before proceeding.

Prerequisites

Before you begin, make sure that you have:

  • Deleted all objects in the bucket, including all current and previous versions if versioning is enabled

    • For a small number of objects, delete them manually. See Delete objects

    • For a large number of objects, configure a lifecycle rule to delete them automatically. See Lifecycle

    • For versioning details, see Versioning

  • Deleted all access points attached to the bucket. See Access points

  • Deleted all parts generated from multipart uploads or resumable uploads. See Delete parts

  • The oss:DeleteBucket permission granted via RAM Policy or Bucket Policy. By default, an Alibaba Cloud account has full permissions. RAM users and RAM roles have no permissions by default

Method definition

delete_bucket(request: DeleteBucketRequest, **kwargs) → DeleteBucketResult

Parameters

ParameterTypeRequiredDescription
requestDeleteBucketRequestYesThe request object. Pass the bucket name via bucket=<bucket-name>. See DeleteBucketRequest

Return value

DeleteBucketResult — contains status_code and request_id. See DeleteBucketResult.

For the complete method reference, see delete_bucket.

Delete a bucket

The following example deletes a bucket. Credentials are loaded from environment variables, and the region and bucket name are passed as command-line arguments.

import argparse
import alibabacloud_oss_v2 as oss

parser = argparse.ArgumentParser(description="Delete a specified OSS bucket.")
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
parser.add_argument('--bucket', help='The name of the bucket to delete.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS.')

def main():
    args = parser.parse_args()

    # Load credentials from environment variables
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region

    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    client = oss.Client(cfg)

    request = oss.DeleteBucketRequest(bucket=args.bucket)

    try:
        result = client.delete_bucket(request)
        print(f'status code: {result.status_code},'
              f' request id: {result.request_id}')
    except oss.exceptions.OssError as e:
        print(f"Failed to delete bucket: {e}")

if __name__ == "__main__":
    main()

Replace the following placeholders when running the script:

PlaceholderDescriptionExample
--regionThe region where the bucket is locatedcn-hangzhou
--bucketThe name of the bucket to deletemy-bucket
--endpoint(Optional) Custom endpoint for cross-service access within the same region. Use an internal endpoint to avoid public network trafficoss-cn-hangzhou-internal.aliyuncs.com
Note

The example uses the China (Hangzhou) region (cn-hangzhou) with a public endpoint by default. To access OSS from other Alibaba Cloud services in the same region, pass the internal endpoint via --endpoint. For supported regions and endpoints, see OSS regions and endpoints.

References