tencent cloud

DELETE Bucket
Last updated:2026-03-05 16:42:26
DELETE Bucket
Last updated: 2026-03-05 16:42:26

Introduction

This document provides an overview of the API for deleting a bucket and SDK sample code.

Must-Knows

Before performing bucket-related operations, you need to have the relevant permissions:
If you want to delete a bucket, when configuring the authorization policy, the action needs to be set to cos:DeleteBucket. For more authorization, see the business APIs that support CAM.
Before deleting a bucket, ensure that all data and incomplete multipart uploads within the bucket have been completely cleared. Otherwise, the bucket cannot be deleted.
After a bucket is deleted, data cannot be recovered. Please ensure the data is no longer in use before deletion. If you need to continue using the data in the bucket, see Cloud Data Backup to back up the data in advance.
After deleting a bucket, you need to wait several hours (typically 4 to 8 hours) before you can recreate a bucket with the same name. During this period, the name becomes available to everyone for creating new buckets. If you wish to continue using the original bucket name, we recommend emptying the bucket rather than deleting it, so as to retain the bucket.

Related Examples

Function Name
Description
Example code
Deleting a Bucket
Delete empty buckets under a specified account.

Preliminary Preparation

Using Permanent Key to Create CosAPI

Before the COS API is called, you must first create an instance of CosAPI for subsequent call requests.
qcloud_cos::CosAPI InitCosAPI() {
uint64_t appid = 12500000000;
std::string region = "ap-guangzhou";// Region of the bucket, see https://www.tencentcloud.com/document/product/436/62?from_cn_redirect=1
std::string secret_id = "************************************"; // User's SecretId. It is recommended to use sub-account keys, with authorization following the least privilege principle to mitigate usage risks. For information on how to obtain sub-account keys, see https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1
std::string secret_key = "************************************"; // User's SecretKey. It is recommended to use sub-account keys, with authorization following the least privilege principle to mitigate usage risks. For information on how to obtain sub-account keys, see https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1
qcloud_cos::CosConfig config(appid, secret_id, secret_key, region);
qcloud_cos::CosAPI cos_tmp(config);
return cos_tmp;
}

Create CosAPI Using a Temporary Key

To access COS with a temporary key, you need to create a CosAPI instance using the temporary key.
qcloud_cos::CosAPI InitCosAPI() {
// You need to have obtained the temporary key results: tmp_secret_id, tmp_secret_key,
// For generating temporary keys, see https://www.tencentcloud.com/document/product/436/14048?from_cn_redirect=1#cos-sts-sdk
uint64_t appid = 12500000000;
std::string region = "ap-guangzhou";
std::string tmp_secret_id = "************************************";
std::string tmp_secret_key = "************************************";
std::string tmp_token = "token";
qcloud_cos::CosConfig config(appid, tmp_secret_id, tmp_secret_key, region);
config.SetTmpToken(tmp_token);
qcloud_cos::CosAPI cos_tmp(config);
return cos_tmp;
}

Use Case: Delete Bucket

Deletes empty bucket(s) of a specified account.

Method Prototype

CosResult CosAPI::DeleteBucket(const DeleteBucketReq& req, DeleteBucketResp* resp)

Request Example

void DeleteBucket(qcloud_cos::CosAPI& cos) {
qcloud_cos::DeleteBucketReq req(bucket_name);
qcloud_cos::DeleteBucketResp resp;
qcloud_cos::CosResult result = cos.DeleteBucket(req, &resp);

std::cout << "===================DeleteBucketResponse====================="
<< std::endl;
PrintResult(result, resp);
std::cout << "========================================================="
<< std::endl;
}

Parameter Description

Parameter Name
Description
Type
req
Delete Bucket Request.
DeleteBucketReq
resp
Delete Bucket Response.
DeleteBucketReq
DeleteBucketReq Member or Function Description:
Member or Function
Description
Parameter Type
bucket_name
Bucket name, which can be set via the constructor or set method.
The naming format for buckets is BucketName-APPID. For details, see Naming Conventions.
string
DeleteBucketReq Member or Function Description:
Member or Function
Description
Parameter Type
GetXCosRequestId
Obtain the request ID.
string

Returning Description

CosResult main member functions are as follows:
Member functions
Description
Return Type
IsSucc
Indicates whether the operation is successful; returns true for success, false for failure.
bool
GetHttpStatus
Obtain the HTTP status code.
int
GetErrorCode
Obtain the error code when the request fails.
string
GetErrorMsg
Obtain the error message when the request fails.
string
GetXCosRequestId
Obtain the request ID.
string
Usage examples for CosResult are as follows. Users may choose to utilize them based on their needs:
void PrintResult(const qcloud_cos::CosResult& result, const qcloud_cos::BaseResp& resp) {
if (result.IsSucc()) {
std::cout << "Request Succ." << std::endl;
std::cout << resp.DebugString() << std::endl;
} else {
std::cout << "ErrorMsg=" << result.GetErrorMsg() << std::endl;
std::cout << "HttpStatus=" << result.GetHttpStatus() << std::endl;
std::cout << "ErrorCode=" << result.GetErrorCode() << std::endl;
std::cout << "ErrorMsg=" << result.GetErrorMsg() << std::endl;
std::cout << "ResourceAddr=" << result.GetResourceAddr() << std::endl;
std::cout << "XCosRequestId=" << result.GetXCosRequestId() << std::endl;
std::cout << "XCosTraceId=" << result.GetXCosTraceId() << std::endl;
}
}

API Operations

For the API documentation on bucket deletion, see the DELETE Bucket document.


Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback