This topic describes how to delete an object, multiple objects, and objects whose names contain a specific prefix.

Warning Deleted objects cannot be recovered. Exercise caution when you delete objects.

Delete an object

The following code provides an example on how to delete an object named exampleobject.txt from a bucket named examplebucket:

#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;

int main(void)
{
    /* Initialize the information about the account that is used to access Object Storage Service (OSS). */
    /* Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console. */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    /* Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. */
    std::string Endpoint = "yourEndpoint";
    /* Specify the name of the bucket. Example: examplebucket. */
    std::string BucketName = "examplebucket";
    /* Specify the full path of the object. Example: exampleobject.txt. The full path of the object cannot contain the bucket name. */
    /* If you want to delete a directory, set ObjectName to the directory name. If the directory is not empty, you must delete all objects in the directory before you delete the directory. */
    std::string ObjectName = "exampleobject.txt";

    /* Initialize resources such as networks. */
    InitializeSdk();

    ClientConfiguration conf;
    OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
   
    DeleteObjectRequest request(BucketName, ObjectName);

    /* Delete the object. */
    auto outcome = client.DeleteObject(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 resources such as networks. */
    ShutdownSdk();
    return 0;
}

Delete multiple objects in a batch

You can delete up to 1,000 specified objects or objects whose names contain a specific prefix at a time.

OSS also allows you to configure lifecycle rules to automatically delete objects. For more information, see Lifecycle rules based on the last modified time in OSS Developer Guide.

The result can be returned in one of the following modes:

  • Verbose mode: A list of objects that are deleted is returned. This is the default mode.
  • Quiet mode: A list of objects that failed to be deleted is returned.
  • Delete multiple specified objects
    The following sample code provides an example on how to delete multiple specified objects from a bucket named examplebucket:
    #include <alibabacloud/oss/OssClient.h>
    using namespace AlibabaCloud::OSS;
    
    int main(void)
    {
        /* Initialize the information about the account that is used to access OSS. */
        /* Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console. */
        std::string AccessKeyId = "yourAccessKeyId";
        std::string AccessKeySecret = "yourAccessKeySecret";
        /* Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. */
        std::string Endpoint = "yourEndpoint";
        /* Specify the name of the bucket. Example: examplebucket. */
        std::string BucketName = "examplebucket";
        /* Specify the full paths of the objects. Example: exampleobject.txt. The full paths of the objects cannot contain the bucket name. */
        std::string ObjectName = "exampleobject.txt";
    
        /* Initialize resources such as networks. */
        InitializeSdk();
    
        ClientConfiguration conf;
        OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
    
        DeleteObjectsRequest request(BucketName);
        /* Specify the full paths of the objects that you want to delete. */
        request.addKey(ObjectName);
    
        /* Delete the objects. */
        auto outcome = client.DeleteObjects(request);
    
        if (!outcome.isSuccess()) {
            /* Handle exceptions. */
            std::cout << "DeleteObjects fail" <<
            ",code:" << outcome.error().Code() <<
            ",message:" << outcome.error().Message() <<
            ",requestId:" << outcome.error().RequestId() << std::endl;
            ShutdownSdk();
            return -1;
        }
    
        /* Release resources such as networks. */
        ShutdownSdk();
        return 0;
    }
  • Delete objects whose names contain a specific prefix
    The following sample code provides an example on how to delete objects whose names contain the "file" prefix from a bucket named examplebucket:
    #include <alibabacloud/oss/OssClient.h>
    using namespace AlibabaCloud::OSS;
    
    int main(void)
    {
        /* Initialize the information about the account that is used to access OSS. */
        /* Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console. */
        std::string AccessKeyId = "yourAccessKeyId";
        std::string AccessKeySecret = "yourAccessKeySecret";
        /* Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. */
        std::string Endpoint = "yourEndpoint";
        /* Specify the name of the bucket. Example: examplebucket. */
        std::string BucketName = "examplebucket";
        /* Specify the prefix. */
        std::string keyPrefix = "file";
    
        /* Initialize resources such as networks. */
        InitializeSdk();
    
        ClientConfiguration conf;
        OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
    
        std::string nextMarker = "";
        bool isTruncated = false;
        do {
                /* List objects. */
                ListObjectsRequest request(BucketName);
                /* Specify the prefix. */
                request.setPrefix(keyPrefix);
                request.setMarker(nextMarker);
                auto outcome = client.ListObjects(request);
    
                if (!outcome.isSuccess()) {
                    /* Handle exceptions. */
                    std::cout << "ListObjects fail" <<
                    ",code:" << outcome.error().Code() <<
                    ",message:" << outcome.error().Message() <<
                    ",requestId:" << outcome.error().RequestId() << std::endl;
                    break;
                }
                for (const auto& object : outcome.result().ObjectSummarys()) {
                    DeleteObjectRequest request(BucketName, object.Key());
                    /* Delete the objects. */
                    auto delResult = client.DeleteObject(request);
                }
                nextMarker = outcome.result().NextMarker();
                isTruncated = outcome.result().IsTruncated();
        } while (isTruncated);
    
        /* Release resources such as networks. */
        ShutdownSdk();
        return 0;
    }