All Products
Search
Document Center

Object Storage Service:Delete files (Python SDK V1)

Last Updated:May 27, 2026

Delete a single object, multiple specified objects, objects whose names contain a specific prefix, or delete a specific directory and all objects in the directory.

Warning

Deleted objects cannot be recovered. Exercise caution when you delete objects.

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

  • To delete an object, you must have the oss:DeleteObject permission. For more information, see Grant a custom policy.

Delete a single file

The following sample code provides an example on how to delete an object named exampleobject.txt from a bucket named examplebucket:

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint for 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 information that corresponds to the endpoint, for example, cn-hangzhou. Note that this parameter is required for V4 signatures.
region = "cn-hangzhou"

# Replace examplebucket with the bucket name.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)

# Delete the file.
# Replace yourObjectName with the full path of the file to delete. The full path does not include the bucket name. For example, exampledir/exampleobject.txt.
# To delete a folder, set yourObjectName to the folder name. Empty the folder before deleting it.
bucket.delete_object('exampledir/exampleobject.txt')            

Delete multiple files

You can manually delete up to 1,000 objects at a time. You can delete multiple specified objects, objects whose names contain the specified prefix, or a directory and all objects in the directory.

You can also configure lifecycle rules to automatically delete objects. For more information, see Last modification time lifecycle rules.

Delete multiple objects with specified names

The following sample code provides an example on how to delete multiple objects with specified names:

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint for 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 information that corresponds to the endpoint, for example, cn-hangzhou. Note that this parameter is required for V4 signatures.
region = "cn-hangzhou"

# Replace examplebucket with the bucket name.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)


# Batch delete three files. You can delete up to 1,000 files in a single request.
# Specify the full paths of the three files to delete. The full paths do not include the bucket name.
result = bucket.batch_delete_objects(['exampleobject1.jpg', 'testobject2.png', 'destobject3.txt'])
# Print the names of the successfully deleted files.
print('\n'.join(result.deleted_keys))            

Delete multiple objects with the specified object name prefix or in the specified directory

The following sample code provides an example on how to delete multiple objects with the specified prefix and how to delete the specified directory and all objects in the directory.

Warning

If the prefix is not specified or is set to NULL in the following sample code, all objects in the bucket are deleted. Exercise caution when you specify a prefix in a delete operation.

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint for 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 information that corresponds to the endpoint, for example, cn-hangzhou. Note that this parameter is required for V4 signatures.
region = "cn-hangzhou"

# Replace examplebucket with the bucket name.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)

# Set the prefix for deletion. Use 'src' to delete all files with 'src' prefix.
# Use 'src/' to delete only the folder 'src' and all files within it.
# prefix = 'src/'

# Delete multiple files.
for obj in oss2.ObjectIterator(bucket, prefix=prefix):
    bucket.delete_object(obj.key)

References