Delete a bucket (Harmony SDK)

Updated at:

A bucket is a container that stores objects. This topic describes how to delete a bucket.

Precautions

  • For a list of supported regions and their endpoints, see Regions and endpoints.

  • Before you delete a bucket, you must delete all files, LiveChannels, and parts from multipart uploads in the bucket.

  • If the bucket has incomplete multipart uploads, you must cancel the uploads using listUploads and abortMultipartUpload before you delete the bucket.

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 policies or Bucket Policy.

API

Action

Description

DeleteBucket

oss:DeleteBucket

Deletes a bucket.

Sample code

Use the following code to delete a specific bucket.

import Client, { RequestError } from '@aliyun/oss';

// Create an OSS client instance.
const client = new Client({
  // Replace with the Access Key ID of your STS temporary credential.
  accessKeyId: 'yourAccessKeyId',
  // Replace with the Access Key Secret of your STS temporary credential.
  accessKeySecret: 'yourAccessKeySecret',
  // Replace with the security token of your STS temporary credential.
  securityToken: 'yourSecurityToken',
  // Specify the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set Region to oss-cn-hangzhou.
  region: 'oss-cn-hangzhou',
});

// Delete the specified bucket.
const deleteBucket = async () => {
  try {
    // Call the deleteBucket method to delete the specified bucket.
    const res = await client.deleteBucket({
      bucket: 'yourBucketName' // Replace with the name of the bucket to delete.
    });

    // Print the response.
    console.log(JSON.stringify(res));
  } catch (err) {
    // Catch and handle request errors.
    if (err instanceof RequestError) {
      console.log('Error code: ', err.code); // Error code. For example, "NoSuchBucket" means the bucket does not exist.
      console.log('Error message: ', err.message); // Error description with more details.
      console.log('Request ID: ', err.requestId); // The unique request ID for troubleshooting.
      console.log('HTTP status code: ', err.status); // HTTP response status code. For example, 404 means the resource was not found.
      console.log('Error category: ', err.ec); // Error category for further classification.
    } else {
      console.log('Unknown error: ', err); // An error that is not a RequestError, such as another type of exception.
    }
  }
};

// Call the function to delete the specified bucket.
deleteBucket();

References

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