If you no longer use an Object Storage Service (OSS) bucket, you can delete the bucket to prevent unnecessary charges.
Usage notes
The sample code in this topic uses the region ID
cn-hangzhouof the China (Hangzhou) region. By default, a public endpoint is used to access resources in a bucket. If you want to access resources in the bucket from other Alibaba Cloud services in the same region, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.The name of a bucket is unique. Once a bucket is deleted, its name becomes available for others to use. We recommend that you empty the bucket instead of deleting it if you still want to use the name.
A deleted bucket cannot be restored. Make sure that the data in the bucket is no longer needed before you delete the bucket. If you want to continue using the data in the bucket, back up the data in advance. For more information, see Back up buckets.
To delete a bucket, you must have the
oss:DeleteBucketpermission. For more information, see Attach a custom policy to a RAM user.
Prerequisites
All objects in the bucket are deleted.
ImportantTo delete a versioning-enabled bucket, make sure that all current and previous versions of objects in the bucket are deleted. For more information, see Versioning.
If the bucket contains a small number of objects, we recommend that you manually delete them. For more information, see Delete objects.
If the bucket contains many objects, we recommend that you configure lifecycle rules to delete the objects. For more information, see Lifecycle.
Parts generated by multipart upload or resumable upload tasks in the bucket are deleted. For more information, see Delete parts.
You must have the
oss:DeleteBucketpermission. For more information, see Attach a custom policy to a RAM user.NoteIf you have the
oss:DeleteBucketpermission in a RAM policy but still cannot delete the bucket, a bucket policy may contain theoss:DeleteBucketpermission with an effect of Deny. You must change the effect from Deny to Allow or delete this bucket policy. Then, you can delete the bucket.
Examples
The following sample code provides an example on how to delete a bucket:
<?php
require_once __DIR__ . '/../vendor/autoload.php'; // Automatically load objects and dependency libraries.
use AlibabaCloud\Oss\V2 as Oss;
// Specify command line parameters.
$optsdesc = [
"region" => ['help' => The region in which the bucket is located.', 'required' => True], // (Required) Specify the region in which the bucket is located. Example: oss-cn-hangzhou.
"endpoint" => ['help' => The domain names that other services can use to access OSS.', 'required' => False], // (Optional) Specify the endpoint that can be used by other services to access OSS.
"bucket" => ['help' => The name of the bucket, 'required' => True], // (Required) Specify the name of the bucket.
];
$longopts = \array_map(function ($key) {
return "$key:"; // Add a colon (:) to the end of each parameter to indicate that a value is required.
}, array_keys($optsdesc));
// Parse the command line parameters.
$options = getopt("", $longopts);
// Check whether the required parameters are configured.
foreach ($optsdesc as $key => $value) {
if ($value['required'] === True && empty($options[$key])) {
$help = $value['help'];
echo "Error: the following arguments are required: --$key, $help"; // Specifies that the required parameters are not configured.
exit(1);
}
}
// Obtain the values of the command line parameters.
$region = $options["region"]; // The region in which the bucket is located.
$bucket = $options["bucket"]; // The name of the bucket.
// Use environment variables to load the credential information (AccessKey ID and AccessKey secret).
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Use the default configurations of the SDK.
$cfg=Oss\Config::loadDefault(); // Load the default configurations of the SDK.
$cfg->setCredentialsProvider($credentialsProvider); // Specify the credential provider.
$cfg->setRegion($region); // Specify the region.
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]); // Specify the endpoint if an endpoint is provided.
}
// Create an OSSClient instance.
$client = new Oss\Client($cfg);
// Create a request to delete the bucket.
$request = new Oss\Models\DeleteBucketRequest($bucket);
// Use the deleteBucket method to delete the bucket.
$result = $client->deleteBucket($request);
// Display the returned result.
printf(
'status code:' . $result-> statusCode. PHP_EOL . // The HTTP response status code.
'request id:' . $result->requestId // The unique identifier of the request.
);
References
For the complete sample code that is used to delete a bucket, visit GitHub.
For more information about the API operation that you can call to delete a bucket, see DeleteBucket.