This topic describes how to delete a single object or multiple objects from a versioning-enabled bucket.

Delete a single object

If you do not specify the version ID when performing the delete_object operation on an object in a versioning-enabled bucket, the current version of the object is retained. A delete marker is added and becomes the current version of the object. If you specify the version ID, the object with the specified version ID is permanently deleted.

  • If you do not specify the version ID:

    By default, the DeleteObject operation is performed on the current version of the target object. The current version is retained and a delete marker is inserted as the new current version of the object. When the GetObject operation is performed on the object, and OSS identifies that the current version of the object is a delete marker, OSS returns 404 Not Found. Additionally, the header: x-oss-delete-marker = true and new version ID x-oss-version-id of the delete marker are included in the response.

    If the value of the x-oss-delete-marker header is true, the version specified in the x-oss-version-id field is a delete marker.

  • If you specify the version ID:

    OSS permanently deletes the object with the specified version ID. For example, if you want to delete an object with a version ID of "null", you must add params['versionId'] = “null” when you set the params parameter. OSS identifies the string "null" as a version ID and deletes the object whose version ID is "null".

The following section provides examples on how to permanently delete an object with a specified version ID and how to temporarily delete an object.
  • Permanently delete an object with a specified version ID
    The following code provides an example on how to permanently delete an object with a specified version ID:
    # -*- 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>'
    
    # Specify the version ID of an object or a delete marker.
    params = dict()
    params['versionId'] = '<yourObjectVersionIdOrDeleteMarkerVersionId>'
    
    # Delete the object with the specified version ID or the delete marker with the specified version ID.
    result = bucket.delete_object(object_name, params=params)
    print("delete object name: ", object_name)
    # If you specify the version ID of an object, the returned value of delete_marker is None, and the specified version ID of the object is returned.
    # If you specify the version ID of a delete marker, the returned value of delete_marker is True, and the specified version ID of the delete marker is returned.
    if result.delete_marker:
        print("delete del-marker versionid: ",result.versionid)
    else:
        print("delete object versionid:", result.versionid)
  • Temporarily delete an object without specifying the version ID
    The following code provides an example on how to temporarily delete an object:
    # -*- 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>'
    
    # Delete an object without specifying the version ID.
    result = bucket.delete_object(object_name)
    # View the delete marker.
    print("delete marker: ", result.delete_marker)
    # View the returned version ID of the delete marker.
    print("delete marker versionid: ", result.versionid)

For more information about deleting a single object, see DeleteObject.

Delete multiple objects

You can perform the batch_delete_objects operation to delete multiple objects from a versioning-enabled bucket. If you do not specify the version IDs in your request, delete markers are added. If you specify the version IDs in your request, objects with the specified version IDs are permanently deleted.

The following section provides examples on how to permanently delete an object with a specified version ID and how to temporarily delete an object.

  • Delete multiple objects without specifying the version IDs
    The following code provides an example on how to delete multiple objects without specifying the version IDs:
    # -*- 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>']
    # After you have deleted multiple objects without specifying the version IDs, delete markers are added.
    result = bucket.batch_delete_objects(key_list)
    for del_version in result.delete_versions:
        print('key name:', del_version.key)
        # Obtain the returned version IDs of the delete markers.
        print('Is del marker:', del_version.delete_marker)
        print('key del_marker.versionid', del_version.delete_marker_versionid)
  • Delete multiple objects by specifying the version IDs
    The following code provides an example on how to delete multiple objects with specified version IDs or delete markers with specified version IDs:
    # -*- 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 objects to delete or the version IDs of delete markers.
    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 with the specified version IDs or the delete markers with the specified version IDs.
    result = bucket.delete_object_versions(version_list)
    # View the version IDs of the deleted objects or the version IDs of the deleted delete markers.
    for del_version in result.delete_versions:
        print('del object name:', del_version.key)
        # Check whether the delete markers are deleted.
        print('Is del marker:', del_version.delete_marker)
        # If the delete markers are deleted, obtain the version IDs of the deleted delete markers. If the objects are deleted, obtain the version IDs of the deleted objects.
        if del_version.delete_marker :
            print('del object del_marker.versionid', del_version.delete_marker_versionid)
        else:
            print('del object versionid:', del_version.versionid)

For more information about deleting multiple objects, see DeleteMultipleObjects.

Delete objects with a specified prefix

The following code provides an example on how to delete an object with 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 all objects with a specified prefix and delete the 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