This topic describes how to delete a single object, multiple objects, or objects whose names contain a specified prefix from a versioned bucket.

Delete operations on a versioned bucket

When you delete an object from a versioned 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 new 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, the x-oss-delete-marker header and the x-oss-version-id header that indicates the version ID of the created delete marker are included in the response.

    The value of the x-oss-delete-marker header in the response is true, which indicates that the x-oss-version-id value 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 version specified by the versionId field in the params parameter. To delete a version whose ID is null, add params['versionId'] = "null" to params in the request. OSS identifies the string "null" as the ID of the version to delete and deletes the version whose ID is null.

Delete a single object

The following examples show how to permanently or temporarily delete a single object from a versioned bucket.

  • Permanently delete an object from a versioned bucket
    The following code provides an example on how to permanently delete an object from a versioned bucket by specifying the version ID of the object in the request:
    # -*- coding: utf-8 -*-
    import os
    import oss2
    # The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret')
    # 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. 
    # Set yourBucketName to the name of the bucket. 
    bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'yourBucketName')
    # yourObjectName specifies the complete path of the object excluding the bucket name. 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 object with the specified version ID or the object whose current version is a delete marker with the specified version ID. 
    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 versioned bucket
    The following code provides an example on how to temporarily delete an object from a versioned bucket by sending a request in which no version ID is specified:
    # -*- coding: utf-8 -*-
    import os
    import oss2
    # The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret')
    # 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. 
    # Set yourBucketName to the name of the bucket. 
    bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'yourBucketName')
    # yourObjectName specifies the complete path of the object excluding the bucket name. 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 show how to permanently or temporarily delete multiple objects from a versioned bucket.

  • Permanently delete multiple objects from a versioned bucket
    The following code provides an example on how to permanently delete multiple objects or delete markers with specified version IDs from a versioned bucket:
    # -*- coding: utf-8 -*-
    import os
    import oss2
    from oss2.models import BatchDeleteObjectVersion
    from oss2.models import BatchDeleteObjectVersionList
    # The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret')
    # 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. 
    # Set yourBucketName to 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, display the version ID of the delete marker. If the deleted object is not a delete marker, display 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 versioned bucket
    The following code provides an example on how to delete multiple objects from a versioned bucket by sending a request in which no version IDs are specified:
    # -*- coding: utf-8 -*-
    import os
    import oss2
    from oss2.models import BatchDeleteObjectVersion
    from oss2.models import BatchDeleteObjectVersionList
    # The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret')
    # 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. 
    # Set yourBucketName to 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 code provides an example on how to delete objects whose names contain a specified prefix:

# -*- coding: utf-8 -*-
import oss2
# The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret')
# 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. 
# Set yourBucketName to 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 a single object, see DeleteObject.
  • For more information about the API operation that you can call to delete multiple objects, see DeleteMultipleObjects.