All Products
Search
Document Center

Object Storage Service:Delete files (C++ SDK)

Last Updated:Mar 19, 2026
Warning

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

Use the OSS C++ SDK to delete a single object, multiple objects by name, or all objects that share a key prefix (including an entire directory).

Prerequisites

Before you begin, ensure that you have:

  • An OSS bucket with objects to delete

  • The oss:DeleteObject permission on the target objects. For details, see Attach a custom policy to a RAM user.

  • The OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables set with valid credentials

SDK initialization

All examples in this topic share the same initialization pattern:

InitializeSdk()
→ ClientConfiguration with SignatureVersionType::V4
→ EnvironmentVariableCredentialsProvider
→ OssClient(Endpoint, credentialsProvider, conf) + SetRegion(Region)
→ [build and execute request]
→ ShutdownSdk()

The main classes used in deletion operations:

ClassPurpose
OssClientMain client — entry point for all OSS operations
DeleteObjectRequestRequest object for deleting a single object
DeleteObjectsRequestRequest object for batch-deleting multiple objects by name
ListObjectsRequestRequest object for listing objects (used in prefix-based deletion)

Usage notes

  • The examples use the public endpoint for the China (Hangzhou) region. To access OSS from another Alibaba Cloud service in the same region, use an internal endpoint instead. For region and endpoint details, see Regions and endpoints.

  • To create an OssClient using a custom domain name or Security Token Service (STS), see Create an OSSClient instance.

Delete a single object

The following example deletes exampleobject.txt from examplebucket.

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

int main(void)
{
    std::string Endpoint   = "yourEndpoint";   // e.g., https://oss-cn-hangzhou.aliyuncs.com
    std::string Region     = "yourRegion";     // e.g., cn-hangzhou
    std::string BucketName = "examplebucket";
    // Specify the full path of the object. For example, exampleobject.txt. The full path cannot contain the bucket name.
    // To delete a folder, set ObjectName to the folder name. If the folder is not empty, you must delete all objects in the folder before you can delete the folder.
    std::string ObjectName = "exampleobject.txt";

    InitializeSdk();

    ClientConfiguration conf;
    conf.signatureVersion = SignatureVersionType::V4;

    // Load credentials from environment variables (OSS_ACCESS_KEY_ID, OSS_ACCESS_KEY_SECRET)
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);
    client.SetRegion(Region);

    DeleteObjectRequest request(BucketName, ObjectName);
    auto outcome = client.DeleteObject(request);

    if (!outcome.isSuccess()) {
        std::cout << "DeleteObject fail"
                  << ", code: "      << outcome.error().Code()
                  << ", message: "   << outcome.error().Message()
                  << ", requestId: " << outcome.error().RequestId() << std::endl;
        return -1;
    }

    ShutdownSdk();
    return 0;
}

For the complete runnable example, see ObjectSample.cc on GitHub.

Delete multiple objects

Batch deletion supports up to 1,000 objects per request. Two response modes are available:

ModeReturns
Verbose mode (default)A list of all deleted objects
Basic modeOnly the objects that failed to be deleted

To delete objects automatically on a schedule, configure lifecycle rules. For details, see Lifecycle rules based on the last modified time.

Delete objects by name

The following example deletes two objects by specifying their full paths.

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

int main(void)
{
    std::string Endpoint   = "yourEndpoint";
    std::string Region     = "yourRegion";
    std::string BucketName = "examplebucket";

    InitializeSdk();

    ClientConfiguration conf;
    conf.signatureVersion = SignatureVersionType::V4;

    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);
    client.SetRegion(Region);

    DeleteObjectsRequest request(BucketName);
    request.addKey("yourObjectName1");  // Full path of the first object to delete
    request.addKey("yourObjectName2");  // Full path of the second object to delete

    auto outcome = client.DeleteObjects(request);

    if (!outcome.isSuccess()) {
        std::cout << "DeleteObjects fail"
                  << ", code: "      << outcome.error().Code()
                  << ", message: "   << outcome.error().Message()
                  << ", requestId: " << outcome.error().RequestId() << std::endl;
        return -1;
    }

    ShutdownSdk();
    return 0;
}

For the complete runnable example, see ObjectSample.cc on GitHub.

Delete objects by key prefix

Warning

If keyPrefix is an empty string or null, all objects in the bucket are deleted.

This approach lists all objects that match a prefix, then deletes them one by one. Use it to delete a directory and all its contents.

Prefix behavior:

keyPrefix valueWhat gets deleted
srcAll objects whose key starts with src, including objects in the src/ directory
src/Only objects inside the src/ directory (the directory itself and all its contents)
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;

int main(void)
{
    std::string Endpoint   = "yourEndpoint";
    std::string Region     = "yourRegion";
    std::string BucketName = "examplebucket";
    std::string keyPrefix  = "src";  // Set to "src/" to delete only the src/ directory

    InitializeSdk();

    ClientConfiguration conf;
    conf.signatureVersion = SignatureVersionType::V4;

    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);
    client.SetRegion(Region);

    std::string nextMarker = "";
    bool isTruncated = false;

    do {
        // List the next page of objects matching the prefix
        ListObjectsRequest listRequest(BucketName);
        listRequest.setPrefix(keyPrefix);
        listRequest.setMarker(nextMarker);
        auto listOutcome = client.ListObjects(listRequest);

        if (!listOutcome.isSuccess()) {
            std::cout << "ListObjects fail"
                      << ", code: "      << listOutcome.error().Code()
                      << ", message: "   << listOutcome.error().Message()
                      << ", requestId: " << listOutcome.error().RequestId() << std::endl;
            break;
        }

        // Delete each object in the current page
        for (const auto& object : listOutcome.result().ObjectSummarys()) {
            DeleteObjectRequest delRequest(BucketName, object.Key());
            client.DeleteObject(delRequest);
        }

        nextMarker  = listOutcome.result().NextMarker();
        isTruncated = listOutcome.result().IsTruncated();
    } while (isTruncated);

    ShutdownSdk();
    return 0;
}

For the complete runnable example, see ObjectSample.cc on GitHub.

API reference

OperationAPI
Delete a single objectDeleteObject
Delete multiple objectsDeleteMultipleObjects