Delete a bucket that you no longer need by using the C# SDK V2.
Prerequisites
-
The sample code uses the China (Hangzhou) region (
cn-hangzhou) and a public endpoint. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For more information, 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 policies or Bucket Policy.
|
API |
Action |
Description |
|
DeleteBucket |
|
Deletes a bucket. |
Sample code
The following code deletes 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 complete sample code, see deleteBucket.cs.