A bucket is a container that stores objects. Delete a bucket when it is no longer needed. Before deletion, you must empty the bucket of all objects, LiveChannels, and incomplete multipart uploads.
Prerequisites
Before you begin, ensure that you have:
Emptied the bucket of all objects, LiveChannels, and parts from incomplete multipart uploads. To cancel incomplete multipart uploads, use
listUploadsandabortMultipartUploadGranted the
oss:DeleteBucketpermission to your Alibaba Cloud account or RAM user through a RAM policy or bucket policy
Alibaba Cloud accounts have full permissions by default. RAM users and RAM roles have no permissions by default and require explicit grants.
Permissions
| API | Action | Description |
|---|---|---|
| DeleteBucket | oss:DeleteBucket | Deletes a bucket |
Delete a bucket
The following example deletes a specified bucket.
const OSS = require('ali-oss');
const client = new OSS({
// Set region to the region where the bucket is located.
// Example: oss-cn-hangzhou for the China (Hangzhou) region.
region: 'oss-cn-hangzhou',
// Load credentials from environment variables.
// Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running this code.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
bucket: 'examplebucket',
});
async function deleteBucket() {
try {
const result = await client.deleteBucket('examplebucket');
console.log('Bucket deleted successfully:', result);
} catch (err) {
console.log(err);
}
}
deleteBucket();Replace the following placeholders with actual values:
| Placeholder | Description | Example |
|---|---|---|
oss-cn-hangzhou | The region where the bucket is located | oss-ap-southeast-1 |
examplebucket | The name of the bucket to delete | my-project-bucket |
Troubleshooting
If bucket deletion fails, check the following:
Bucket is not empty: Delete all objects, LiveChannels, and parts from incomplete multipart uploads. Use
listUploadsto list incomplete multipart uploads andabortMultipartUploadto cancel them.Insufficient permissions: Make sure the
oss:DeleteBucketaction is granted in the RAM policy or bucket policy.
What's next
For the complete sample code, see the GitHub example.
For the API reference, see DeleteBucket.