Delete a bucket when you no longer need it to avoid unnecessary charges.
Deletion is permanent and irreversible.
The bucket name is released after deletion and can be registered by other users. To keep the name, empty the bucket instead of deleting it.
All data in the bucket is permanently lost. Back up any data you need before proceeding. For details, see Back up a bucket.
Prerequisites
Before you begin, ensure that you have:
Cleaned up all resources in the bucket. For details, see Delete a bucket.
The
oss:DeleteBucketpermission granted to your Alibaba Cloud account, RAM user, or RAM role — via RAM policy or Bucket Policy.
Permissions
By default, an Alibaba Cloud account has full permissions. RAM users and RAM roles have no permissions by default. Grant the following permission before running the code.
| API | Action | Description |
|---|---|---|
| DeleteBucket | oss:DeleteBucket | Deletes a bucket. |
Code example
The following example deletes a bucket named examplebucket.
#include "oss_api.h"
#include "aos_http_io.h"
/* Replace yourEndpoint with the endpoint for your bucket's region.
Example: https://oss-cn-hangzhou.aliyuncs.com for China (Hangzhou). */
const char *endpoint = "yourEndpoint";
/* Replace yourRegion with the region ID of your bucket.
Example: cn-hangzhou for China (Hangzhou). */
const char *region = "yourRegion";
/* The bucket to delete. */
const char *bucket_name = "examplebucket";
void init_options(oss_request_options_t *options)
{
options->config = oss_config_create(options->pool);
/* Initialize aos_string_t fields from char* strings. */
aos_str_set(&options->config->endpoint, endpoint);
/* Credentials are read from environment variables.
Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running. */
aos_str_set(&options->config->access_key_id, getenv("OSS_ACCESS_KEY_ID"));
aos_str_set(&options->config->access_key_secret, getenv("OSS_ACCESS_KEY_SECRET"));
aos_str_set(&options->config->region, region);
options->config->signature_version = 4;
/* Set is_cname to 0 when not using a CNAME to access OSS. */
options->config->is_cname = 0;
/* Set network parameters such as timeout. */
options->ctl = aos_http_controller_create(options->pool, 0);
}
int main(int argc, char *argv[])
{
/* Initialize global network and memory resources. */
if (aos_http_io_initialize(NULL, 0) != AOSE_OK) {
exit(1);
}
/* Create a memory pool for this request. */
aos_pool_t *pool;
aos_pool_create(&pool, NULL);
/* Create and initialize the client options. */
oss_request_options_t *oss_client_options;
oss_client_options = oss_request_options_create(pool);
init_options(oss_client_options);
/* Initialize request parameters. */
aos_string_t bucket;
aos_table_t *resp_headers = NULL;
aos_status_t *resp_status = NULL;
aos_str_set(&bucket, bucket_name);
/* Delete the bucket. */
resp_status = oss_delete_bucket(oss_client_options, &bucket, &resp_headers);
if (aos_status_is_ok(resp_status)) {
printf("delete bucket succeeded\n");
} else {
printf("delete bucket failed\n");
}
/* Release the memory pool and global resources. */
aos_pool_destroy(pool);
aos_http_io_deinitialize();
return 0;
}Usage notes
The example uses the public endpoint for the China (Hangzhou) region. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint instead. For endpoint details, see Regions and endpoints.
The example creates an OSSClient instance using an OSS endpoint. To use a custom domain name or Security Token Service (STS), see Initialization.
References
DeleteBucket — API reference for the delete-bucket operation.