This topic describes how to delete a bucket using the C# SDK V2.
Prerequisites
The sample code in this topic uses the China (Hangzhou) region, which has the region ID
cn-hangzhou, as an example. By default, a public endpoint is used. If you want to access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For more information about the regions and endpoints that OSS supports, see Regions and endpoints.All access points for the bucket are deleted. For more information, see Access points.
All objects in the bucket are deleted.
ImportantIf versioning is enabled for the bucket, make sure that all current and previous versions of objects in the bucket are deleted. For more information, see Versioning.
If you have a small number of objects, you can manually delete them. For more information, see Delete objects.
If you have many objects, you can configure a lifecycle rule to automatically delete them. For more information, see Lifecycle.
All parts that are generated by multipart or resumable uploads in the bucket are deleted. For more information, see Delete parts.
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 Policy.
API | Action | Definition |
DeleteBucket |
| Deletes a bucket. |
Sample code
You can use the following code to delete a bucket.
using OSS = AlibabaCloud.OSS.V2; // Create an alias for Alibaba Cloud OSS SDK to simplify subsequent use.
var region = "cn-hangzhou"; // Required. Specify the region where the bucket is located. In this example, China (Hangzhou) is used. Set the region to cn-hangzhou.
var bucket = "your bucket name"; // Required. Specify the name of the destination bucket.
var endpoint = null as string; // Optional. Specify the domain name used to access OSS. In this example, China (Hangzhou) is used. Set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
// Load the default configurations of the OSS SDK. The configurations automatically read credential information, such as the AccessKey pair, from environment variables.
var cfg = OSS.Configuration.LoadDefault();
// Explicitly configure the use of environment variables to obtain credentials for identity verification. The format is OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET.
cfg.CredentialsProvider = new OSS.Credentials.EnvironmentVariableCredentialsProvider();
// Specify the region of the bucket in the configurations.
cfg.Region = region;
// If an endpoint is specified, overwrite the default endpoint.
if(endpoint != null)
{
cfg.Endpoint = endpoint;
}
// Create an OSS client instance based on the configuration information.
using var client = new OSS.Client(cfg);
// Call the DeleteBucketAsync method to delete the specified bucket.
var result = await client.DeleteBucketAsync(new OSS.Models.DeleteBucketRequest()
{
Bucket = bucket
});
// Print the result information.
Console.WriteLine("DeleteBucket done"); // A message indicating that the operation is complete.
Console.WriteLine($"StatusCode: {result.StatusCode}"); // The HTTP status code.
Console.WriteLine($"RequestId: {result.RequestId}"); // The request ID, which is used for troubleshooting in Alibaba Cloud.
Console.WriteLine("Response Headers:"); // The response header.
result.Headers.ToList().ForEach(x => Console.WriteLine(x.Key + " : " + x.Value)); // Traverse and print all response headers.References
For the complete sample code for deleting a bucket, see deleteBucket.cs.