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 a version ID in a DeleteObject request, OSS does not delete the current version of the object but add a delete marker to the object as the new current version. If you perform the GetObject operation on the object, OSS identifies that the current version of the object is a delete marker, and then 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 to the DeleteObject request.

    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 a version ID in a DeleteObject 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 with a specified version ID from a versioned bucket:

    #include <alibabacloud/oss/OssClient.h>
    using namespace AlibabaCloud::OSS;
    
    int main(void)
    {
        /* Initialize the OSS account information. */
        std::string AccessKeyId = "yourAccessKeyId";
        std::string AccessKeySecret = "yourAccessKeySecret";
        std::string Endpoint = "yourEndpoint";
        std::string BucketName = "yourBucketName";
        std::string ObjectName = "yourObjectName";
    
        /* Initialize network resources. */
        InitializeSdk();
    
        ClientConfiguration conf;
        OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
    
        /* Delete the object with the specified version ID or the object whose current version is a delete marker with the specified version ID. */
        auto outcome = client.DeleteObject(BucketName, ObjectName, "yourObjectVersionIdOrDeleteMarkerVersionId");
        /* 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 (! outcome.isSuccess()) {
            /* Handle exceptions. */
            std::cout << "DeleteObject fail" <<
            ",code:" << outcome.error().Code() <<
            ",message:" << outcome.error().Message() <<
            ",requestId:" << outcome.error().RequestId() << std::endl;
            ShutdownSdk();
            return -1;
        }
    
        /* Release network resources. */
        ShutdownSdk();
        return 0;
    }
  • 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:

    #include <alibabacloud/oss/OssClient.h>
    using namespace AlibabaCloud::OSS;
    
    int main(void)
    {
        /* Initialize the OSS account information. */
        std::string AccessKeyId = "yourAccessKeyId";
        std::string AccessKeySecret = "yourAccessKeySecret";
        std::string Endpoint = "yourEndpoint";
        std::string BucketName = "yourBucketName";
        std::string ObjectName = "yourObjectName";
    
        /* Initialize network resources. */
        InitializeSdk();
    
        ClientConfiguration conf;
        OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
    
        DeleteObjectRequest request(BucketName, ObjectName);
    
        /* Temporarily delete an object without specifying its version ID. A delete marker is added to the object. */
        auto outcome = client.DeleteObject(request);
        /* Display the version ID of the added delete marker. */
        if (outcome.isSuccess()) {
            std::cout << "versionid:" << outcome.result().VersionId() << ",DeleteMarker:" << outcome.result().DeleteMarker() << std::endl;
        }
        else {
            /* Handle exceptions. */
            std::cout << "PutObject fail" <<
            ",code:" << outcome.error().Code() <<
            ",message:" << outcome.error().Message() <<
            ",requestId:" << outcome.error().RequestId() << std::endl;
            ShutdownSdk();
            return -1;
        }
    
        /* Release network resources. */
        ShutdownSdk();
        return 0;
    }

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 delete the objects with specified version IDs or the objects whose current versions are delete markers with specified version IDs:

    #include <alibabacloud/oss/OssClient.h>
    using namespace AlibabaCloud::OSS;
    
    int main(void)
    {
        /* Initialize the OSS account information. */
        std::string AccessKeyId = "yourAccessKeyId";
        std::string AccessKeySecret = "yourAccessKeySecret";
        std::string Endpoint = "yourEndpoint";
        std::string BucketName = "yourBucketName";
        std::string ObjectName = "yourObjectName";
    
        /* Initialize network resources. */
        InitializeSdk();
    
        ClientConfiguration conf;
        OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
    
        DeleteObjectVersionsRequest  request(BucketName);
    
        /* Specify the version IDs of the objects or delete markers you want to delete. */
        ObjectIdentifier obj1("yourObject1Name");
        obj1.setVersionId("yourVersionId");
    
        ObjectIdentifier obj2("yourObject2Name");
        obj2.setVersionId("obj2_del_marker_versionid");
    
        request.addObject(obj1);
        request.addObject(obj2);
    
        /* Delete the objects with specified version IDs or the objects whose current versions are delete markers with specified version IDs. */
        auto outcome = client.DeleteObjectVersions(request);
    
        if (! outcome.isSuccess()) {
            /* Handle exceptions. */
            std::cout << "DeleteObjectVersions fail" <<
            ",code:" << outcome.error().Code() <<
            ",message:" << outcome.error().Message() <<
            ",requestId:" << outcome.error().RequestId() << std::endl;
            ShutdownSdk();
            return -1;
        }
    
        /* Release network resources. */
        ShutdownSdk();
        return 0;
    }

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

  • 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:

    #include <alibabacloud/oss/OssClient.h>
    using namespace AlibabaCloud::OSS;
    
    int main(void)
    {
        /* Initialize the OSS account information. */
        std::string AccessKeyId = "yourAccessKeyId";
        std::string AccessKeySecret = "yourAccessKeySecret";
        std::string Endpoint = "yourEndpoint";
        std::string BucketName = "yourBucketName";
        std::string ObjectName = "yourObjectName";
    
        /* Initialize network resources. */
        InitializeSdk();
    
        ClientConfiguration conf;
        OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
    
        DeleteObjectVersionsRequest request(BucketName);
        /* Specify the names of the objects that you want to delete. */
        ObjectIdentifier obj1(ObjectName1);
        ObjectIdentifier obj2(ObjectName2);
        ObjectIdentifier obj3(ObjectName3);
    
        request.addObject(obj1);
        request.addObject(obj2);
        request.addObject(obj3);
    
        /* Delete the objects. */
        auto outcome = client.DeleteObjectVersions(request);
    
        if (outcome.isSuccess()) {
            for (auto const &obj : outcome.result().DeletedObjects()) {
                std::cout << "versionid:" << obj.VersionId() << ",DeleteMarker:" << obj.DeleteMarker() << std::endl;
            }
        }
        else {
            /* Handle exceptions. */
            std::cout << "DeleteObjectVersions fail" <<
            ",code:" << outcome.error().Code() <<
            ",message:" << outcome.error().Message() <<
            ",requestId:" << outcome.error().RequestId() << std::endl;
            ShutdownSdk();
            return -1;
        }
    
    
        /* Release network resources. */
        ShutdownSdk();
        return 0;
    }

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:

#include <alibabacloud/oss/OssClient.h>

using namespace AlibabaCloud::OSS;

int main(void)
{
    /* Initialize the OSS account information. */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    std::string Endpoint = "yourEndpoint";
    std::string BucketName = "yourBucketName";
    std::string ObjectName = "yourObjectName";

    /* Initialize network resources. */
    InitializeSdk();

    ClientConfiguration conf;
    OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);

    ListObjectVersionsRequest request(BucketName);
    bool IsTruncated = false;

    do {
            request.setPrefix("yourkeyPrefix");
            auto outcome = client.ListObjectVersions(request);

            if (outcome.isSuccess()) {
                /* Display the versions of the listed objects and delete markers. */
                for (auto const &marker : outcome.result().DeleteMarkerSummarys()) {
                   client.DeleteObject(DeleteObjectRequest(bucket, marker.Key(), marker.VersionId()));
                }

                /* List all versions of the objects whose names contain the specified prefixes and delete the versions. */
                for (auto const &obj : outcome.result().ObjectVersionSummarys()) {
                     client.DeleteObject(DeleteObjectRequest(bucket, obj.Key(), obj.VersionId()));
                }
            }
            else {
                std::cout << "ListObjectVersions fail" <<
                ",code:" << outcome.error().Code() <<
                ",message:" << outcome.error().Message() <<
                ",requestId:" << outcome.error().RequestId() << std::endl;
                break;
            }
            request.setKeyMarker(outcome.result().NextKeyMarker());
            request.setVersionIdMarker(outcome.result().NextVersionIdMarker());
            IsTruncated = outcome.result().IsTruncated();
    } while (IsTruncated);

    /* Release network resources. */
    ShutdownSdk();
    return 0;
}