Deleting Buckets

Last updated: 2023-09-13 11:16:16

Feature Overview

This document provides an overview of APIs and SDK code samples for deleting a bucket.
API
Operation
Description
Deleting a bucket
Deletes an empty bucket under a specified account

Deleting a bucket

Note

This API (DELETE Bucket) is used to delete a specified bucket.
Note
Before deleting a bucket, ensure that all data and unfinished uploaded parts within the bucket have been cleared; otherwise, the bucket cannot be deleted.

Sample code

Method prototype

public Guzzle\Service\Resource\Model deleteBucket(array $args = array());

Sample Request

<?php

require dirname(__FILE__) . '/../vendor/autoload.php';

$secretId = "SECRETID"; //Replace it with the actual SecretId, which can be viewed and managed in the CAM console at https://console.cloud.tencent.com/cam/capi
$secretKey = "SECRETKEY"; //Replace it with the actual SecretKey, which can be viewed and managed in the CAM console at https://console.cloud.tencent.com/cam/capi
$region = "ap-beijing"; //Replace it with the actual region, which can be viewed in the console at https://console.cloud.tencent.com/cos5/bucket
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https', //Protocol header, default is http
'credentials'=> array(
'secretId' => $secretId ,
'secretKey' => $secretKey)));

try {
$result = $cosClient->deleteBucket(array(
'Bucket' => 'examplebucket-125000000' // Bucket name, composed of BucketName-Appid, can be viewed in the COS console at https://console.cloud.tencent.com/cos5/bucket
));
// Request successful
print_r($result);
} catch (\Exception $e) {
// Request failed
echo($e);
}

Description

Parameter name
Parent Node
Description
Local Disk Types
Bucket
-
Bucket name in the format of BucketName-APPID
String

Deleting All Buckets

Note

This API (DELETE Buckets) is used to delete all buckets.

Sample code

Method prototype

public Guzzle\Service\Resource\Model deleteBuckets(array $args = array());

Sample Request

<?php

require dirname(__FILE__) . '/../vendor/autoload.php';

$secretId = "SECRETID"; //Replace it with the actual SecretId, which can be viewed and managed in the CAM console at https://console.cloud.tencent.com/cam/capi
$secretKey = "SECRETKEY"; //Replace it with the actual SecretKey, which can be viewed and managed in the CAM console at https://console.cloud.tencent.com/cam/capi
$region = "ap-beijing"; //Replace it with the actual region, which can be viewed in the console at https://console.cloud.tencent.com/cos5/bucket
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https', //Protocol header, default is http
'credentials'=> array(
'secretId' => $secretId ,
'secretKey' => $secretKey)));
try {
// List all buckets
$buckets = $cosClient->listBuckets();

// If the bucket list is not empty, perform the deletion logic: first, delete the objects, then check if there are any uploaded tasks to be deleted, and finally, delete the bucket.
// You can add exclude logic in the foreach loop (e.g., create an array of buckets to be retained) to keep only the necessary buckets.
if (!empty($buckets['Buckets'][0])) {
foreach ($buckets['Buckets'][0]['Bucket'] as $key => $value) {
$result = $cosClient->listObjects(array('Bucket' => $value['Name']));
if (isset($result['Contents'])) {
foreach ($result['Contents'] as $content) {
$cosClient->deleteObject(array('Bucket' => $value['Name'], 'Key' => $content['Key']));
}
}
while(True){
$result = $cosClient->ListMultipartUploads(
array('Bucket' => $value['Name']));
if ($result['Uploads'] == array()) {
break;
}
foreach ($result['Uploads'] as $upload) {
try {
$cosClient->AbortMultipartUpload(
array('Bucket' => $value['Name'],
'Key' => $upload['Key'],
'UploadId' => $upload['UploadId']));
} catch (\Exception $e) {
print_r($e);
}
}
}
$cosClient->deleteBucket(array('Bucket' => $value['Name']));
}
}
print_r('DELETE ALL BUCKETS SUCCEED!');
} catch (\Exception $e) {
// Request failed
echo($e);
}

Description

Parameter name
Parent Node
Description
Local Disk Types
Bucket
-
Bucket name in the format of BucketName-APPID
String