Delete a bucket when you no longer need it to stop billing and make sure no billable resources remain. Note that deleted data cannot be recovered, and the bucket name becomes available for other users to register.
Bucket deletion is irreversible. Before you delete a bucket:
Back up any data you still need. See Back up a bucket.
If you want to keep the bucket name, delete the bucket's contents instead of the bucket itself. Once deleted, the name is released and can be registered by others.
To stop using OSS entirely, delete all buckets under your Alibaba Cloud account.
Prerequisites
Before you begin, make sure that:
An
OSSClientinstance is initialized using a custom domain name or Security Token Service (STS). See Initialize an OSSClient instance for Android.All resources in the bucket are deleted. See Delete resources.
Permissions
By default, an Alibaba Cloud account has full permissions. RAM users and RAM roles have no permissions by default and must be granted access via RAM Policy or bucket policy.
| API | Action | Description |
|---|---|---|
| DeleteBucket | oss:DeleteBucket | Deletes a bucket |
If you holdoss:DeleteBucketin a RAM policy but still cannot delete the bucket, a bucket policy may haveoss:DeleteBucketset to Deny. Change the effect to Allow or remove that bucket policy before retrying.
Delete a bucket
The following code submits an asynchronous delete request and handles both success and failure callbacks.
DeleteBucketRequest deleteBucketRequest = new DeleteBucketRequest("<bucket-name>");
// Submit an asynchronous delete request.
OSSAsyncTask deleteBucketTask = oss.asyncDeleteBucket(deleteBucketRequest, new OSSCompletedCallback<DeleteBucketRequest, DeleteBucketResult>() {
@Override
public void onSuccess(DeleteBucketRequest request, DeleteBucketResult result) {
Log.d("asyncDeleteBucket", "Success!");
}
@Override
public void onFailure(DeleteBucketRequest request, ClientException clientException, ServiceException serviceException) {
if (clientException != null) {
// Client-side error, such as a network failure.
clientException.printStackTrace();
}
if (serviceException != null) {
// Server-side error returned by OSS.
Log.e("ErrorCode", serviceException.getErrorCode());
Log.e("RequestId", serviceException.getRequestId());
Log.e("HostId", serviceException.getHostId());
Log.e("RawMessage", serviceException.getRawMessage());
}
}
});Replace <bucket-name> with the name of the bucket to delete.
On success, onSuccess is triggered and logs the following to Logcat:
asyncDeleteBucket: Success!On failure, onFailure logs the error code, request ID, host ID, and raw message to help you diagnose the issue.
For the complete test suite, see the GitHub example.