All Products
Search
Document Center

Object Storage Service:Delete a bucket (C++ SDK)

Last Updated:Mar 20, 2026

Delete a bucket when you no longer need it or want to stop billing for it. OSS charges are mainly for resources within a bucket. Deleting the bucket is the most reliable way to make sure you don't miss any billable resources and incur unexpected charges. To completely stop using the OSS service, you must delete all buckets under your account.

Warning

Deletion is permanent. Data in a deleted bucket cannot be recovered. Before you delete a bucket, confirm that you no longer need the data, or back it up. The bucket name is also released and becomes available for other users to register. To keep the bucket name, delete the objects in the bucket instead of the bucket itself.

Prerequisites

Before you begin, make sure that:

  • The bucket is empty — Delete all resources in the bucket before deleting the bucket. See Delete the required resources for details.

  • You have the oss:DeleteBucket permission — By default, Alibaba Cloud accounts have full permissions. RAM users and RAM roles have no permissions by default; the account owner or administrator must grant them via a RAM policy or bucket policy.

  • No bucket policy is blocking the deletion — If a RAM policy grants oss:DeleteBucket but deletion still fails, a bucket policy may contain a Deny statement for oss:DeleteBucket. Change the effect of that statement from Deny to Allow, or delete the bucket policy.

Permissions

APIActionDescription
DeleteBucketoss:DeleteBucketDeletes a bucket

Delete a bucket

The following example deletes a bucket named examplebucket.

Note

This example uses the public endpoint for the China (Hangzhou) region. To access OSS from another Alibaba Cloud service in the same region, use the internal endpoint instead. For endpoint details, see Regions and endpoints. To create an OSSClient instance using a custom domain name or Security Token Service (STS), see Create an OSSClient instance.

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

int main(void)
{
    // Set the endpoint for the region where the bucket is located.
    // Example: https://oss-cn-hangzhou.aliyuncs.com for China (Hangzhou).
    std::string Endpoint = "yourEndpoint";

    // Set the region ID. Example: cn-hangzhou for China (Hangzhou).
    std::string Region = "yourRegion";

    // Set the bucket name.
    std::string BucketName = "examplebucket";

    // Initialize network resources.
    InitializeSdk();

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

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

    // Delete the bucket.
    DeleteBucketRequest request(BucketName);
    auto outcome = client.DeleteBucket(request);

    if (outcome.isSuccess()) {
        std::cout << "Delete bucket successfully." << std::endl;
    } else {
        std::cout << "Failed to delete bucket. Error code: " << outcome.error().Code()
                  << ", Message: " << outcome.error().Message()
                  << ", RequestId: " << outcome.error().RequestId() << std::endl;
    }

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

Replace the following placeholders with your actual values:

PlaceholderDescriptionExample
yourEndpointEndpoint for the region where the bucket is locatedhttps://oss-cn-hangzhou.aliyuncs.com
yourRegionRegion IDcn-hangzhou
examplebucketName of the bucket to delete

Set the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables to your Alibaba Cloud access credentials before running the code.

What's next