Delete a bucket (OSS C++ SDK V2)

更新时间:
复制 MD 格式

You can delete buckets you no longer need to avoid unnecessary charges. This topic shows how to delete a bucket by using the OSS C++ SDK V2.

Usage notes

  • Before you run the example code, replace <region> with a valid region, such as cn-hangzhou.

  • The example code in this topic reads access credentials from an environment variable.

  • To delete a bucket, you must have the oss:DeleteBucket permission.

  • To delete a bucket, you must first delete all its objects, including parts from incomplete multipart uploads.

Warning

Deleted buckets cannot be recovered. Proceed with caution.

Example code

The following code shows how to delete a specified bucket:

#include <iostream>
#include "alibabacloud/oss2/ClientConfiguration.h"
#include "alibabacloud/oss2/OSSClient.h"
#include "alibabacloud/oss2/credentials/CredentialsProvider.h"

namespace oss = alibabacloud::oss2;

int main() {
    auto conf = oss::ClientConfiguration::loadDefault();
    conf.region = "<region>";
    conf.credentialsProvider = std::make_shared<oss::EnvironmentVariableCredentialsProvider>();

    oss::OSSClient client(conf);

    auto outcome = client.deleteBucket(
        oss::models::DeleteBucketRequest()
            .setBucket("examplebucket"));

    if (!outcome.has_value()) {
        auto& e = outcome.error();
        std::cerr << "Failed to delete the bucket"
                  << ", code: " << e.getCode()
                  << ", message: " << e.getMessage()
                  << ", requestId: " << e.getRequestId() << std::endl;
        return 1;
    }

    auto& result = outcome.value();
    std::cout << "Bucket deleted successfully"
              << ", status: " << result.getStatusCode()
              << ", requestId: " << result.getRequestId() << std::endl;
    return 0;
}
        

References