If you no longer use a bucket, delete the bucket to stop unnecessary charges.

Warning Deleted buckets cannot be recovered. Exercise caution when you perform this operation.

Prerequisites

  • All objects in the bucket are deleted.
    • For more information about how to manually delete a small number of objects, see Delete objects.
    • If you want to delete a large number of objects, we recommend that you configure lifecycle rules to delete the objects in batches. For more information about how to delete a large number of objects, see Configure lifecycle rules.
    Notice

    If versioning is enabled for the bucket that you want to delete, make sure that all versions of objects in the bucket are deleted. For more information about how to delete all versions of objects in a bucket, see Configure versioning.

  • Parts generated by multipart upload or resumable upload tasks in the bucket are deleted. For more information about how to delete parts in a bucket, see Manage parts.
  • All LiveChannels in the bucket are deleted. For more information about how to delete LiveChannels, see DeleteLiveChannel.

Sample code

The following code provides an example on how to delete a bucket:

DeleteBucketRequest deleteBucketRequest = new DeleteBucketRequest("bucketName");

// Delete the bucket asynchronously. 
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) {
        // Handle request exceptions. 
        if (clientException != null) {
            // Handle client exceptions, such as network exceptions. 
            clientException.printStackTrace();
        }
        if (serviceException != null) {
            // Handle service exceptions. 
            Log.e("ErrorCode", serviceException.getErrorCode());
            Log.e("RequestId", serviceException.getRequestId());
            Log.e("HostId", serviceException.getHostId());
            Log.e("RawMessage", serviceException.getRawMessage());
        }
    }
});

References

  • For more information about 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.