You can delete a bucket when you no longer need it or want to stop billing for it. OSS charges are mainly for resources within a bucket. Before you can delete a bucket, you must clear all its resources. Deleting the bucket is the most reliable way to ensure that you do not miss any billable resources and incur unexpected fees. Note that data cannot be recovered after deletion. The bucket name also becomes available for other users to register. To completely stop using the OSS service, you must delete all buckets under your account.
After a bucket is deleted, its name is released and can be used by other users. If you want to retain the bucket name, delete the content of the bucket instead of the bucket itself.
Data in a deleted bucket cannot be recovered. Before you delete a bucket, confirm that you no longer need the data. If you want to continue using the data in the bucket, back it up first. For more information about backups, see Back up a bucket.
Usage notes
In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.
In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Initialization.
Before you delete a bucket, ensure that you have deleted the required resources.
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 Policy or Bucket policies.
API | Action | Definition |
DeleteBucket |
| Deletes a bucket. |
Sample code
The following sample code shows how to delete a bucket named examplebucket:
using System;
using Aliyun.OSS;
using Aliyun.OSS.Common;
namespace Samples
{
public class Program
{
public static void Main(string[] args)
{
// Set endpoint to the Endpoint of the region where your bucket is located. For example, if your bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com.
var endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Set bucketName to the name of your bucket. Example: examplebucket.
var bucketName = "examplebucket314";
// Set region to the ID of the region where your bucket is located. For example, if your bucket is in the China (Hangzhou) region, set the region to cn-hangzhou.
const string region = "cn-hangzhou";
// Create a ClientConfiguration instance and modify the default parameters as needed.
var conf = new ClientConfiguration();
// Set the signature version to V4.
conf.SignatureVersion = SignatureVersion.V4;
// Create an 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);
}
}
}
}References
For more information about the API operation that you can call to delete a bucket, see DeleteBucket.