Delete a bucket when you no longer need it. Because OSS charges are based on the resources inside a bucket, deleting the bucket is the most reliable way to stop all associated billing and avoid unexpected fees.
Deletion is permanent. After a bucket is deleted:
All data in the bucket is unrecoverable.
The bucket name is released and may be registered by other users. To retain the name, delete the objects inside the bucket instead of the bucket itself.
To stop using OSS entirely, delete all buckets under your account.
Prerequisites
Before you begin, ensure that you have:
Cleared all resources from the bucket (see Resources to clear before deleting a bucket)
The
oss:DeleteBucketpermission granted through RAM Policy or bucket policyThe
OSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRETenvironment variables configured
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 policy.
| API | Action | Definition |
|---|---|---|
| DeleteBucket | oss:DeleteBucket | Deletes a bucket. |
Resources to clear before deleting a bucket
A bucket must be empty before it can be deleted. For most users, this means deleting all objects. If you use advanced OSS features, you may also need to remove the following resources.
For all users:
All objects in the bucket. For a small number of objects, delete them manually. For large volumes, configure a lifecycle rule to expire and remove objects automatically.
If versioning is enabled, delete all current and previous versions of each object.
For users of advanced features:
Parts from multipart uploads or resumable uploads — see Delete parts
Access points — see Access points
Object FC access points — see Object FC access points
Accelerators — see Create, modify, and delete an accelerator
LiveChannels — see DeleteLiveChannel
For a guided checklist, use the OSS console delete flow, which detects remaining resources automatically.
Delete a bucket
Method:
$ossClient->deleteBucket($bucket);Parameter: $bucket — the name of the bucket to delete (required).
Example:
The following example deletes the bucket 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;
// Load credentials from environment variables.
// Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running this example.
$provider = new EnvironmentVariableCredentialsProvider();
// Set the endpoint for the region where the bucket is located.
// This example uses the China (Hangzhou) region.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Set the bucket name.
$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);
print(__FUNCTION__ . ": OK\n");
} catch (OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
}The example uses the public endpoint for the China (Hangzhou) region. If you access OSS from another Alibaba Cloud service in the same region, use the internal endpoint instead. For a complete list of regions and endpoints, see Regions and endpoints. To initialize OssClient with a custom domain name or Security Token Service (STS), see Create an OssClient instance.What's next
For the complete bucket sample code, see GitHub.
For the API reference, see DeleteBucket.