This topic describes how to delete a single object, multiple objects, or objects whose names contain a specified prefix from a versioning-enabled bucket.
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, endpoints and open ports.
In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials.
In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Initialization.
To delete an object, you must have the
oss:DeleteObject
permission. For more information, see Attach a custom policy to a RAM user.
Delete operations on a versioning-enabled bucket
When you delete an object from a versioning-enabled bucket, you must determine whether to specify a version ID in the request.
Delete an object without specifying a version ID (temporary deletion)
By default, if you do not specify the version ID of the object that you want to delete in the request, Object Storage Service (OSS) does not delete the current version of the object but adds a delete marker to the object as the latest version. If you perform the GetObject operation on the object, OSS identifies the current version of the object as a delete marker and returns
404 Not Found
. In addition,header:x-oss-delete-marker = true
andx-oss-version-id
that indicates the version ID of the delete marker are included in the response.If the value of
x-oss-delete-marker
is true, the value ofx-oss-version-id
is the version ID of a delete marker.Delete an object by specifying a version ID (permanent deletion)
If you specify the version ID of the object that you want to delete in the request, OSS permanently deletes the specific version of the object based on the
versionId
parameter specified inparams
. To delete the version whose ID is null, addparams['versionId'] = "null"
toparams
. OSS identifies the string "null" as the ID of the version to delete and deletes the version whose ID is null.
Delete an object
The following examples show how to permanently or temporarily delete an object from a versioning-enabled bucket.
Permanently delete an object from a versioning-enabled bucket
The following sample code provides an example on how to permanently delete an object from a versioning-enabled bucket by specifying the version ID of the object in the request:
# -*- coding: utf-8 -*- import os import oss2 from oss2.credentials import EnvironmentVariableCredentialsProvider # Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider()) # Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. # Specify the name of the bucket. bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'yourBucketName') # Specify the full path of the object. Do not include the bucket name in the full path. Example: example/test.txt. object_name = 'yourObjectName' # Specify the version ID of the object or delete marker that you want to delete. params = dict() params['versionId'] = 'yourObjectVersionIdOrDeleteMarkerVersionId' # Delete the specific version of the object or delete marker. result = bucket.delete_object(object_name, params=params) print("delete object name: ", object_name) # If the version ID of an object is specified in the request, the value of the delete_marker header in the response is None and the value of the versionId header in the response is the version ID specified in the request. # If the version ID of a delete marker is specified in the request, the value of the delete_marker header in the response is True and the value of the versionId header in the response is the version ID specified in the request. if result.delete_marker: print("delete del-marker versionid: ",result.versionid) else: print("delete object versionid:", result.versionid)
Temporarily delete an object from a versioning-enabled bucket
The following sample code provides an example on how to temporarily delete an object from a versioning-enabled bucket by sending a request in which no version ID is specified:
# -*- coding: utf-8 -*- import os import oss2 from oss2.credentials import EnvironmentVariableCredentialsProvider # Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider()) # Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. # Specify the name of the bucket. bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'yourBucketName') # Specify the full path of the object. Do not include the bucket name in the full path. Example: example/test.txt. object_name = 'yourObjectName' # Temporarily delete an object without specifying a version ID. A delete marker is added to the object. result = bucket.delete_object(object_name) # Display the delete marker added to the object. print("delete marker: ", result.delete_marker) # Display the version ID of the added delete marker. print("delete marker versionid: ", result.versionid)
Delete multiple objects
The following examples describe how to permanently or temporarily delete multiple objects from a versioning-enabled bucket.
Permanently delete multiple objects from a versioning-enabled bucket
The following sample code provides an example on how to permanently delete multiple objects or delete markers from a versioning-enabled bucket by specifying the version IDs of the objects or delete markers in the request:
# -*- coding: utf-8 -*- import os import oss2 from oss2.credentials import EnvironmentVariableCredentialsProvider from oss2.models import BatchDeleteObjectVersion from oss2.models import BatchDeleteObjectVersionList # Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider()) # Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. # Specify the name of the bucket. bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'yourBucketName') version_list = BatchDeleteObjectVersionList() # Specify the version IDs of the objects or delete markers that you want to delete. obj1_versionid = 'yourObject1VersionId' obj1_del_marker_versionid = 'yourObject1DelMarkerVersionId' obj2_versionid = 'yourObject2VersionId' obj2_del_marker_versionid = 'yourObject2DelMarkerVersionId' version_list.append(BatchDeleteObjectVersion(key='yourObject1Name', versionid=obj1_versionid)) version_list.append(BatchDeleteObjectVersion(key='yourObject1Name', versionid=obj1_del_marker_versionid)) version_list.append(BatchDeleteObjectVersion(key='yourObject2Name', versionid=obj2_versionid)) version_list.append(BatchDeleteObjectVersion(key='yourObject2Name', versionid=obj2_del_marker_versionid)) # Delete the objects or delete markers with the specified version IDs. result = bucket.delete_object_versions(version_list) # Display the version IDs of the deleted objects or delete markers. for del_version in result.delete_versions: print('del object name:', del_version.key) # Check whether the deleted objects are delete markers. print('Is del marker:', del_version.delete_marker) # If a deleted object is a delete marker, show the version ID of the delete marker. If the deleted object is not a delete marker, show the version ID of the deleted object. if del_version.delete_marker : print('del object del_marker.versionid', del_version.delete_marker_versionid) else: print('del object versionid:', del_version.versionid)
Temporarily delete multiple objects from a versioning-enabled bucket
The following sample code provides an example on how to temporarily delete multiple objects from a versioning-enabled bucket by sending a request in which no version IDs are specified:
# -*- coding: utf-8 -*- import os import oss2 from oss2.credentials import EnvironmentVariableCredentialsProvider from oss2.models import BatchDeleteObjectVersion from oss2.models import BatchDeleteObjectVersionList # Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider()) # Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. # Specify the name of the bucket. bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'yourBucketName') key_list = ['yourObject1Name', 'yourObject2Name'] # If you delete an object without specifying a version ID in the request, a delete marker is added to the object. result = bucket.batch_delete_objects(key_list) for del_version in result.delete_versions: print('key name:', del_version.key) # Display the returned delete markers. print('Is del marker:', del_version.delete_marker) print('key del_marker.versionid', del_version.delete_marker_versionid)
Delete objects whose names contain a specified prefix
The following sample code provides an example on how to delete objects whose names contain a specified prefix:
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
# Specify the name of the bucket.
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'yourBucketName')
prefix = "yourKeyPrefix"
# List the version IDs of the objects whose names contain the specified prefix and delete these objects.
next_key_marker = None
next_versionid_marker = None
while True:
result = bucket.list_object_versions(prefix=prefix, key_marker=next_key_marker, versionid_marker=next_versionid_marker)
for version_info in result.versions:
bucket.delete_object(version_info.key, params={'versionId': version_info.versionid})
for del_marker_info in result.delete_marker:
bucket.delete_object(del_marker_info.key, params={'versionId': del_marker_info.versionid})
is_truncated = result.is_truncated
if is_truncated:
next_key_marker = result.next_key_marker
next_versionid_marker = result.next_versionid_marker
else:
break
References
For more information about the API operation that you can call to delete an object, see DeleteObject.
For more information about the API operation that you can call to delete multiple objects, see DeleteMultipleObjects.