All Products
Search
Document Center

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

Last Updated:Nov 29, 2025

You can 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. Before you can delete a bucket, you must clear all its resources. Deleting the bucket is the most reliable way to ensure that you do not miss any billable resources and incur unexpected fees. Note that data cannot be recovered after deletion. The bucket name also becomes available for other users to register. To completely stop using the OSS service, you must delete all buckets under your account.

Warning
  • After a bucket is deleted, its name is released and becomes available for other users to register. If you want to reserve the bucket name, delete the objects in the bucket instead of deleting the bucket.

  • Data in a deleted bucket cannot be recovered. Before you delete a bucket, confirm that you no longer need the data. If you want to continue to use the data in the bucket, you must back it up. For more information about backups, see Back up a bucket.

Usage notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.

  • In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient by using custom domain names or Security Token Service (STS), see Create an OSSClient instance.

  • Before you delete a bucket, make sure that you have deleted the required resources.

Permissions

By default, an Alibaba Cloud account has full permissions. RAM users or RAM roles under an Alibaba Cloud account do not have any permissions by default. The Alibaba Cloud account or account administrator must grant operation permissions through RAM Policy or Bucket policies.

API

Action

Definition

DeleteBucket

oss:DeleteBucket

Deletes a bucket.

Note

If you have the oss:DeleteBucket permission in a RAM policy but still cannot delete the bucket, the bucket policy may contain a statement that explicitly denies the oss:DeleteBucket permission. In this case, you must change the effect of the statement from Deny to Allow, or delete the bucket policy before you can delete the bucket.

Sample code

The following sample code shows how to delete a bucket named examplebucket:

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

int main(void)
{
    /*Initialize the OSS account information.*/
    
    /*Set yourEndpoint to the Endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com.*/
    std::string Endpoint = "yourEndpoint";
    
    /*Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou.*/
    std::string Region = "yourRegion";
    
    /*Specify the bucket name, for example, examplebucket.*/
    std::string BucketName = "examplebucket";

    /*Initialize network resources.*/
    InitializeSdk();

    ClientConfiguration conf;
    conf.signatureVersion = SignatureVersionType::V4;
    /*Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.*/
    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;
}

References

  • For the complete sample code for deleting a bucket, see GitHub.

  • For more information about the API operation that you can call to delete a bucket, see DeleteBucket.