Delete an Object Storage Service (OSS) bucket when you no longer need it to avoid unnecessary charges.
Deletion is permanent and cannot be undone. Back up any data you still need before proceeding. For more information, see Back up buckets.
Usage notes
The sample code in this topic uses region ID
cn-hangzhou(China (Hangzhou) region) and a public endpoint. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint instead. For more information, see Regions and endpoints.Bucket names are unique. Once a bucket is deleted, its name becomes available for other users. To keep the name, empty the bucket instead of deleting it.
If you have the
oss:DeleteBucketpermission in a RAM policy but still cannot delete the bucket, a bucket policy may be denying the operation. Change the effect of that bucket policy fromDenytoAllow, or delete the bucket policy.
Prerequisites
Before you begin, ensure that you have:
Deleted all objects in the bucket. For versioning-enabled buckets, this includes all current and previous versions of objects.
For a small number of objects, delete them manually. See Delete objects.
For a large number of objects, configure lifecycle rules to delete them. See Lifecycle.
For versioned objects, see Versioning.
Deleted all parts generated by multipart upload or resumable upload tasks. See Delete parts.
The
oss:DeleteBucketpermission for your RAM user or role. See Attach a custom policy to a RAM user.
Delete a bucket
The following example deletes a bucket using the PHP SDK V2.
<?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) Example: oss-cn-hangzhou.
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False], // (Optional)
"bucket" => ['help' => 'The name of the bucket.', 'required' => True], // (Required)
];
$longopts = \array_map(function ($key) {
return "$key:"; // Add a colon (:) to indicate that a value is required.
}, array_keys($optsdesc));
// Parse command line parameters.
$options = getopt("", $longopts);
// Check that required parameters are provided.
foreach ($optsdesc as $key => $value) {
if ($value['required'] === True && empty($options[$key])) {
$help = $value['help'];
echo "Error: the following arguments are required: --$key, $help";
exit(1);
}
}
// Get parameter values.
$region = $options["region"]; // The region in which the bucket is located.
$bucket = $options["bucket"]; // The name of the bucket.
// Load credentials from environment variables (AccessKey ID and AccessKey secret).
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Load the default SDK configuration.
$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider); // Set the credential provider.
$cfg->setRegion($region); // Set the region.
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]); // Set the endpoint if provided.
}
// Create an OSS client.
$client = new Oss\Client($cfg);
// Send the delete bucket request.
try {
$request = new Oss\Models\DeleteBucketRequest($bucket);
$result = $client->deleteBucket($request);
printf(
'status code: %s' . PHP_EOL . // HTTP response status code. Expected: 204
'request ID: %s' . PHP_EOL, // Unique identifier of the request.
$result->statusCode,
$result->requestId
);
// Example output:
// status code: 204
// request ID: 6745F7E5B51A4154
} catch (\Exception $e) {
// Common errors:
// - BucketNotEmpty: the bucket still contains objects or parts.
// - AccessDenied: your credentials lack the oss:DeleteBucket permission,
// or a bucket policy is denying the operation.
echo 'Error: ' . $e->getMessage() . PHP_EOL;
exit(1);
}Run the script from the command line:
php DeleteBucket.php --region oss-cn-hangzhou --bucket <your-bucket-name>Replace <your-bucket-name> with the name of the bucket to delete.
References
Complete sample code: DeleteBucket.php on GitHub
API reference: DeleteBucket