All Products
Search
Document Center

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

Last Updated:Jul 31, 2025

This topic describes how to delete a bucket using Python SDK V2.

Prerequisites

  • The sample code in this topic uses the China (Hangzhou) region as an example. The region ID is cn-hangzhou. By default, a public endpoint is used. If you want to access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For more information about the regions and endpoints that are supported by OSS, see OSS regions and endpoints.

  • All access points of the bucket are deleted. For more information, see Access points.

  • All objects in the bucket are deleted.

    Important

    If versioning is enabled for the bucket, make sure that all current and previous versions of objects in the bucket are deleted. For more information, see Versioning.

    • If you have a small number of objects, you can manually delete them. For more information, see Delete objects.

    • If you have many objects, you can configure a lifecycle rule to automatically delete them. For more information, see Lifecycle.

  • All parts that are generated from multipart uploads or resumable uploads in the bucket are deleted. For more information, see Delete parts.

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 Policy.

API

Action

Definition

DeleteBucket

oss:DeleteBucket

Deletes a bucket.

Method definition

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

Request parameters

Parameter

Type

Description

request

DeleteBucketRequest

The request parameters. For more information, see DeleteBucketRequest

Return values

Type

Description

DeleteBucketResult

The return value. For more information, see DeleteBucketResult

For the complete definition of the delete_bucket method, see delete_bucket.

Sample code

You can use the following code to delete a bucket.

import argparse
import alibabacloud_oss_v2 as oss

# Create a command-line argument parser and describe that this script is used to delete a specified OSS bucket.
parser = argparse.ArgumentParser(description="Delete a specified OSS bucket.")

# Add the --region command-line argument, which specifies the region where the bucket is located. This argument is required.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)

# Add the --bucket command-line argument, which specifies the name of the bucket. This argument is required.
parser.add_argument('--bucket', help='The name of the bucket to delete.', required=True)

# Add the --endpoint command-line argument, which specifies the domain name that other services can use to access OSS. This argument is not required.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS.')

def main():
    """
    The main function, which is used to parse command-line arguments and delete the specified bucket.
    """

    args = parser.parse_args()  # Parse command-line arguments.

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

    # Use the default configurations of the SDK and set the credential provider and region.
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region

    # If the endpoint parameter is provided, set the endpoint in the configuration.
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # Create an OSS client based on the configurations.
    client = oss.Client(cfg)

    # Construct a request to delete the specified bucket.
    request = oss.DeleteBucketRequest(bucket=args.bucket)
   
    try:
        # Send the request and obtain the response.
        result = client.delete_bucket(request)

        # Print the status code and request ID of the response.
        print(f'status code: {result.status_code},'
              f' request id: {result.request_id}')
    except oss.exceptions.OssError as e:
        # Catch and print possible exceptions.
        print(f"Failed to delete bucket: {e}")

if __name__ == "__main__":
    main()  # The script entry point. The main function is called when the file is run.
  

References