All Products
Search
Document Center

Object Storage Service:Delete a bucket

Last Updated:Oct 17, 2023

A bucket is a container that is used to store objects in Object Storage Service (OSS). All objects in OSS are contained in buckets. This topic describes how to delete a bucket.

Note
  • Before you delete a bucket, you must delete all objects in the bucket, LiveChannel objects, and fragments generated by multipart uploads. For more information about the API operation that you can call to delete a LiveChannel object, see DeleteLiveChannel.

  • To delete a bucket in which incomplete multipart upload tasks exist, you must first call listUploads and abortMultipartUpload to cancel the tasks.

Sample code

The following sample code provides an example on how to delete a specified bucket:

const OSS = require('ali-oss');

const client = new OSS({
  // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou. 
  region: 'yourregion',
  // Obtain access credentials from environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET. 
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET
});

async function deleteBucket() {
  try {
    // Specify the name of the bucket. 
    const result = await client.deleteBucket('yourbucketname');
    console.log(result);
  } catch (err) {
    console.log(err);
  }
}

deleteBucket();

References

  • For the complete sample code that is used to delete a bucket, visit GitHub.

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