If you no longer need a bucket, you can delete it to stop all billing. Because Object Storage Service (OSS) fees are charged for the resources inside a bucket, you must clear all resources before deleting it. Deleting a bucket is the most reliable way to prevent unexpected fees from overlooked billable resources. Note that data cannot be recovered after a bucket is deleted, and another user can claim its name after it is released. To completely stop using OSS, you must delete all buckets in 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.
-
This topic demonstrates creating an OSSClient instance with an OSS endpoint. For alternative configurations, such as using a custom domain or authenticating with credentials from Security Token Service (STS), see Create an OssClient.
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 provides an example of how to delete the 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.