All Products
Search
Document Center

Object Storage Service:Delete files (OSS SDK for Python V2)

Last Updated:Mar 20, 2026

Use the OSS SDK for Python V2 to delete a single object or a batch of objects from a bucket.

Prerequisites

Before you begin, ensure that you have:

  • An OSS bucket with at least one object to delete

  • The oss:DeleteObject permission on the target bucket. For setup instructions, see Grant custom permissions to a RAM user

  • The OSS SDK for Python V2 installed and credentials configured via environment variables

The sample code defaults to the China (Hangzhou) region (cn-hangzhou) with a public endpoint. To access OSS from another Alibaba Cloud service in the same region, use an internal endpoint instead. See Regions and endpoints for endpoint mappings.

Method definitions

Delete a single object

delete_object(request: DeleteObjectRequest, **kwargs) -> DeleteObjectResult

Key request fields (DeleteObjectRequest):

FieldTypeRequiredDescription
bucketstrYesThe name of the bucket
keystrYesThe name of the object to delete

Key response fields (DeleteObjectResult):

FieldTypeDescription
status_codeintHTTP status code of the request
request_idstrUnique ID for the request, used for troubleshooting
version_idstrVersion ID of the deleted object (versioned buckets only)
delete_markerboolWhether a delete marker was created (versioned buckets only)

For the full API definition, see delete_object and DeleteObjectRequest.

Delete multiple objects

delete_multiple_objects(request: DeleteMultipleObjectsRequest, **kwargs) -> DeleteMultipleObjectsResult

Key request fields (DeleteMultipleObjectsRequest):

FieldTypeRequiredDescription
bucketstrYesThe name of the bucket
objectslist[DeleteObject]YesThe list of objects to delete
encoding_typestrNoSet to 'url' to URL-encode object keys in the response

Key response fields (DeleteMultipleObjectsResult):

FieldTypeDescription
status_codeintHTTP status code of the request
request_idstrUnique ID for the request
deleted_objectslistDetails of each successfully deleted object
deleted_objects[n].keystrKey of the deleted object
deleted_objects[n].version_idstrVersion ID of the deleted object
deleted_objects[n].delete_markerboolWhether a delete marker was created
deleted_objects[n].delete_marker_version_idstrVersion ID of the delete marker
encoding_typestrEncoding type of object keys in the response

For the full API definition, see delete_multiple_objects and DeleteMultipleObjectsRequest.

Delete a single object

import argparse
import alibabacloud_oss_v2 as oss

# Create a command-line argument parser.
parser = argparse.ArgumentParser(description="delete object sample")
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.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
parser.add_argument('--key', help='The name of the object.', required=True)

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)

    result = client.delete_object(oss.DeleteObjectRequest(
        bucket=args.bucket,
        key=args.key,
    ))

    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
          f' version id: {result.version_id},'
          f' delete marker: {result.delete_marker},'
    )

if __name__ == "__main__":
    main()

For the complete runnable sample, see delete_object.py.

Delete multiple objects

Pass a list of DeleteObject instances to delete_multiple_objects. Set encoding_type='url' so that object keys containing special characters are correctly decoded in the response.

import argparse
import alibabacloud_oss_v2 as oss

parser = argparse.ArgumentParser(description="delete multiple objects sample")
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.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
parser.add_argument('--key', help='The name of the first object.', required=True)
parser.add_argument('--key2', help='The name of the second object.', required=True)

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

    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)

    # Build the list of objects to delete.
    # To delete more objects, add more DeleteObject entries to the list.
    objects = [oss.DeleteObject(key=args.key), oss.DeleteObject(key=args.key2)]

    result = client.delete_multiple_objects(oss.DeleteMultipleObjectsRequest(
        bucket=args.bucket,
        encoding_type='url',
        objects=objects,
    ))

    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
          f' key: {result.deleted_objects[0].key},'
          f' version id: {result.deleted_objects[0].version_id},'
          f' delete marker: {result.deleted_objects[0].delete_marker},'
          f' delete marker version id: {result.deleted_objects[0].delete_marker_version_id},'
          f' encoding type: {result.encoding_type},'
    )

if __name__ == "__main__":
    main()

For the complete runnable sample, see delete_multiple_objects.py.

References