Delete a bucket (Harmony SDK)
Permanently removes a bucket from OSS.
Prerequisites
Before you begin, make sure that:
The bucket is empty — Delete all objects, LiveChannels, and parts from incomplete multipart uploads before deleting the bucket. Cancel incomplete multipart uploads using
listUploadsandabortMultipartUpload.You have the required permission — By default, Alibaba Cloud accounts have full permissions. RAM users and RAM roles have no permissions by default. Grant the
oss:DeleteBucketaction through a RAM Policy or bucket policy. If you cannot delete a bucket, contact your account administrator to confirm you have theoss:DeleteBucketpermission.You know the bucket's region — For supported regions and endpoints, see Regions and endpoints.
Permissions
| API | Action | Description |
|---|---|---|
| DeleteBucket | oss:DeleteBucket | Deletes a bucket. |
Delete a bucket
The following example deletes a bucket using STS temporary credentials.
import Client, { RequestError } from '@aliyun/oss';
// Initialize the OSS client with STS temporary credentials.
const client = new Client({
accessKeyId: 'yourAccessKeyId', // AccessKey ID of your STS temporary credential
accessKeySecret: 'yourAccessKeySecret', // AccessKey secret of your STS temporary credential
securityToken: 'yourSecurityToken', // Security token of your STS temporary credential
region: 'oss-cn-hangzhou', // Region where the bucket is located
});
const deleteBucket = async () => {
try {
const res = await client.deleteBucket({
bucket: 'yourBucketName' // Replace with the bucket name to delete
});
console.log(JSON.stringify(res));
} catch (err) {
if (err instanceof RequestError) {
console.log('Error code: ', err.code); // e.g., "NoSuchBucket" if the bucket does not exist
console.log('Error message: ', err.message); // Detailed error description
console.log('Request ID: ', err.requestId); // Unique ID for troubleshooting
console.log('HTTP status code: ', err.status); // e.g., 404 means resource not found
console.log('Error category: ', err.ec); // Error classification
} else {
console.log('Unknown error: ', err);
}
}
};
deleteBucket();Replace the following placeholders before running the code:
| Placeholder | Description | Example |
|---|---|---|
yourAccessKeyId | AccessKey ID of your STS temporary credential | LTAI5tXxx |
yourAccessKeySecret | AccessKey secret of your STS temporary credential | xXxXxXx |
yourSecurityToken | Security token of your STS temporary credential | — |
oss-cn-hangzhou | Region where the bucket is located | oss-cn-hangzhou |
yourBucketName | Name of the bucket to delete | my-bucket |