Delete a bucket when you no longer need it or want to stop billing for it. OSS charges are based on resources within a bucket, so deleting the bucket is the most reliable way to avoid unexpected fees. To completely stop using OSS, delete all buckets under your account.
After a bucket is deleted, its name is released and can be used by other users. To keep the bucket name, delete the bucket contents instead of the bucket itself.
Deleted bucket data cannot be recovered. Confirm you no longer need the data before proceeding. If you want to keep the data, back up the bucket first.
Prerequisites
Before you begin, ensure that you have:
Deleted all required resources inside the bucket
The
oss:DeleteBucketpermission on the bucket, granted via RAM Policy or bucket policy
Usage notes
The examples in this topic use the public endpoint for the China (Hangzhou) region. To access OSS from another Alibaba Cloud service in the same region, use an internal endpoint.
The examples create an
OssClientinstance using an OSS endpoint. To create the client using a custom domain name or Security Token Service (STS), see Initialization.
Permissions
By default, an Alibaba Cloud account has full permissions. RAM users and RAM roles have no permissions by default. Grant the required permissions using a RAM Policy or bucket policy.
| API | Action | Definition |
|---|---|---|
| DeleteBucket | oss:DeleteBucket | Deletes a bucket |
Delete a bucket
using System;
using Aliyun.OSS;
using Aliyun.OSS.Common;
namespace Samples
{
public class Program
{
public static void Main(string[] args)
{
// Set the endpoint for the region where your bucket is located.
// Example: https://oss-cn-hangzhou.aliyuncs.com for China (Hangzhou).
var endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Load credentials from environment variables.
// Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running this sample.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Set the bucket name and region ID.
var bucketName = "examplebucket314";
const string region = "cn-hangzhou";
// Configure the client to use signature version V4.
var conf = new ClientConfiguration();
conf.SignatureVersion = SignatureVersion.V4;
// Create the OssClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);
try
{
client.DeleteBucket(bucketName);
Console.WriteLine("Delete bucket succeeded");
}
catch (Exception ex)
{
Console.WriteLine("Delete bucket failed. {0}", ex.Message);
}
}
}
}