Delete a bucket (Harmony SDK)

Updated at:

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 listUploads and abortMultipartUpload.

  • 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:DeleteBucket action through a RAM Policy or bucket policy. If you cannot delete a bucket, contact your account administrator to confirm you have the oss:DeleteBucket permission.

  • You know the bucket's region — For supported regions and endpoints, see Regions and endpoints.

Permissions

APIActionDescription
DeleteBucketoss:DeleteBucketDeletes 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:

PlaceholderDescriptionExample
yourAccessKeyIdAccessKey ID of your STS temporary credentialLTAI5tXxx
yourAccessKeySecretAccessKey secret of your STS temporary credentialxXxXxXx
yourSecurityTokenSecurity token of your STS temporary credential
oss-cn-hangzhouRegion where the bucket is locatedoss-cn-hangzhou
yourBucketNameName of the bucket to deletemy-bucket

What's next