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 you delete a bucket, its name is released and can be used by other users. If you want to retain the bucket name, delete the contents of the bucket instead of the bucket itself.
Data in a bucket cannot be recovered after the bucket is deleted. Before you delete a bucket, make sure that you no longer need the data it contains. If you want to continue using the data, back it up in advance. For more information about backups, see Data protection overview.
Resources to delete before deleting a bucket
Before you delete a bucket, you must delete its resources. For most users, this only includes the files in the bucket. For users of advanced features, this may also include other configuration items. The OSS console can automatically detect the resources that you need to delete. For more information, see Delete a bucket.
(Resources that most users need to delete) All files in the bucket.
For a small number of files, you can delete the files manually. For many files, you can use a lifecycle rule. If versioning is enabled for the bucket, you must delete all current and previous versions of the files.
Precautions
In this topic, the public endpoint of the China (Hangzhou) region is used. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For details about supported 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 Create an OSSClient instance.
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:
<?php
if (is_file(__DIR__ . '/../autoload.php')) {
require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
require_once __DIR__ . '/../vendor/autoload.php';
}
use OSS\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use OSS\CoreOssException;
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
$provider = new EnvironmentVariableCredentialsProvider();
// Specify the Endpoint of the region where the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Specify the bucket name. Example: examplebucket.
$bucket= "examplebucket";
try{
$config = array(
"provider" => $provider,
"endpoint" => $endpoint,
"signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
"region"=> "cn-hangzhou"
);
$ossClient = new OssClient($config);
$ossClient->deleteBucket($bucket);
} catch(OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
print(__FUNCTION__ . ": OK" . "\n"); References
For the complete sample code that is used to delete a bucket, see GitHub.
For more information about the API operation that you can call to delete a bucket, see DeleteBucket.