This topic describes how to delete a single or multiple objects and objects whose names contain a specified prefix from a versioning-enabled bucket.

Delete a single object

If you do not specify the version ID when you perform the DeleteObject 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 specified version of the object is permanently deleted.

Note
  • If you do not specify the version ID when you perform the DeleteObject operation:

    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 x-oss-delete-marker = true and x-oss-version-id headers are included in the response. x-oss-version-id indicates the version ID of the delete marker.

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

  • If you specify the version ID when you perform the DeleteObject operation:

    OSS permanently deletes the object by specifying the version ID. For example, if you want to delete an object with a version ID of null, you must set the value of the params['versionId'] parameter in your request to null. 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 by specifying a version ID and how to temporarily delete an object.
  • Permanently delete an object by specifying a version ID

    The following code provides an example on how to permanently delete an object by specifying a version ID:

    #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 an object by specifying a version ID or a delete marker by specifying a version ID. */
        auto outcome = client.DeleteObject(BucketName, ObjectName, "yourObjectVersionIdOrDeleteMarkerVersionId");
        /* If you specify the version ID of an object, the returned value of delete_marker is None. 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. The specified version ID of the delete marker is returned. */
        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 without specifying the version ID

    The following code provides an example on how to temporarily delete an object without specifying the version ID:

    #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);
    
        /* Delete an object without specifying the version ID. */
        auto outcome = client.DeleteObject(request);
        /* View the returned version ID of the 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 deleting a single object, see DeleteObject.

Delete multiple objects

You can call the DeleteObjectVersions 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 delete multiple objects without specifying the version ID or by specifying the version ID:

  • Delete multiple objects without specifying the version ID

    The following code provides an example on how to delete multiple objects without specifying the version ID:

    #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 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 multiple objects by specifying the version IDs

    The following code provides an example on how to delete multiple objects by specifying the version IDs or delete markers by specifying the 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 objects to delete or the version IDs of delete markers. */
        ObjectIdentifier obj1("yourObject1Name");
        obj1.setVersionId("yourVersionId");
    
        ObjectIdentifier obj2("yourObject2Name");
        obj2.setVersionId("obj2_del_marker_versionid");
    
        request.addObject(obj1);
        request.addObject(obj2);
    
        /* Delete the objects by specifying the version IDs or the delete markers by specifying the 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 deleting 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:

#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()) {
                /* View the version information of listed objects whose version IDs are delete markers. */
                for (auto const &marker : outcome.result().DeleteMarkerSummarys()) {
                   client.DeleteObject(DeleteObjectRequest(bucket, marker.Key(), marker.VersionId()));
                }

                /* List the versions of all objects whose names contain a specified prefix 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;
}