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 in versioned bucket

When you delete an object from a versioned bucket, you must determine whether to specify a version ID in the request.

  • Do not specify a version ID (temporary deletion)

    By default, if you do not specify the version ID of the object you want to delete in the request, OSS does not delete the current version of the object and instead add a delete marker to the object as the new current 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 is the version ID of a delete marker.

  • Specify a version ID (permanent deletion)

    If you specify the version ID of the object you want to delete in the request, OSS permanently deletes the version whose ID is specified by the versionId field in the params parameter. To delete a version whose ID is null, add params['versionId'] = "null" in 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 describe 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
    
    # Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console.
    auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
    # The endpoint of the China (Hangzhou) region is used in this example. Specify the actual endpoint.
    bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')
    # Specify the complete name of the object excluding the bucket name. Example: example/test.txt.
    object_name = '<yourObjectName>'
    
    # Specify the version ID of the object or delete marker 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 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:
    # -*- coding: utf-8 -*-
    import os
    import oss2
    
    # Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console.
    auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
    # The endpoint of the China (Hangzhou) region is used in this example. Specify the actual endpoint.
    bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')
    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)

For more information about how to delete a single object, see DeleteObject.

Delete multiple objects

The following examples describe 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
    
    # Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console.
    auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
    # The endpoint of the China (Hangzhou) region is used in this example. Specify the actual endpoint.
    bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')
    
    version_list = BatchDeleteObjectVersionList()
    # Specify the version IDs of the objects or delete markers 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)
        # Display 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 temporarily delete multiple objects from a versioned bucket:
    # -*- coding: utf-8 -*-
    import os
    import oss2
    from oss2.models import BatchDeleteObjectVersion
    from oss2.models import BatchDeleteObjectVersionList
    
    # Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console.
    auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
    # The endpoint of the China (Hangzhou) region is used in this example. Specify the actual endpoint.
    bucket = oss2.Bucket(auth, 'http://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)

For more information about how to delete multiple objects, see DeleteMultipleObjects.

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

# Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console.
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# The endpoint of the China (Hangzhou) region is used in this example. Specify the actual endpoint.
bucket = oss2.Bucket(auth, 'http://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