All Products
Search
Document Center

Object Storage Service:Delete buckets by using OSS SDK for Android

Last Updated:Feb 20, 2024

If you no longer use an Object Storage Service (OSS) bucket, you can delete the bucket to stop unnecessary charges.

Usage notes

  • Before you run the sample code in this topic, you must create an OSSClient instance by using methods such as using a custom domain name or Security Token Service (STS). For more information, see Initialization.

Warning

Deleted buckets cannot be restored. Make sure that the data in the buckets is no longer needed before you delete the buckets. If you want to continue using the data in the buckets, back up the data in advance. For more information, see Back up buckets.

Prerequisites

  • The access points of the bucket are deleted. For more information, see Overview.

  • All objects in the bucket are deleted.

    Important

    To delete a versioning-enabled bucket, make sure that all current and previous versions of objects in the bucket are deleted. For more information, see Overview.

    • 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 a large number of objects, we recommend that you configure lifecycle rules to delete the objects. For more information, see Overview.

  • Parts generated by multipart upload or resumable upload tasks in the bucket are deleted. For more information, see Delete parts.

  • All LiveChannels in the bucket are deleted. For more information, see DeleteLiveChannel.

  • The oss:DeleteBucket permission is granted to a RAM user if you want to use the RAM user to delete the bucket. For more information, see Attach a custom policy to a RAM user.

    Note

    If the RAM user has the oss:DeleteBucket permission in a RAM policy but cannot delete the bucket, a bucket policy may contain the oss:DeleteBucket permission whose effect is Deny. In this case, you need to change Deny to Allow or delete the bucket policy. Then, you can delete the bucket.

Examples

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

DeleteBucketRequest deleteBucketRequest = new DeleteBucketRequest("bucketName");

// Asynchronously delete the bucket. 
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 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.

  • For more information about how to initialize an OSSClient instance, see Initialization.