All Products
Search
Document Center

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

Last Updated:Apr 11, 2025

If you no longer use an Object Storage Service (OSS) bucket, you can delete the bucket to prevent 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
  • Once a bucket is deleted, its name becomes available for others to use. We recommend that you empty the bucket instead of deleting it if you still want to use the name.

  • A deleted bucket cannot be restored. Make sure that the data in the bucket is no longer needed before you delete the bucket. If you want to continue using the data in the bucket, perform backup in advance. For more information, see Back up buckets.

Permissions

By default, an Alibaba Cloud account has full permissions on resources in the account. In contrast, RAM users and RAM roles associated with an Alibaba Cloud account initially have no permissions. To manage resources by using a RAM user or role, you must grant the required permissions via RAM policies or Bucket policies.

API

Action

Description

DeleteBucket

oss:DeleteBucket

Grants the permission to delete a bucket.

Note

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

Resources to clean up before deleting buckets

Before deleting a bucket, all resources stored within must be cleaned up. In most cases, only objects are stored within buckets. However, a small number of users also need to delete configuration items associated with advanced features. We recommend that you use the OSS console, for it supports automatic detection of resources to clean up before deleting buckets.

  • All objects stored stored within the bucket to delete (which applies to the majority of users)

    If the bucket contains a small number of objects, we recommend that you manually delete them. To delete a large number of objects, we recommend that you configure lifecycle rules to delete the objects. In addition, if versioning is enabled for the bucket, both current and historical versions of objects are required to be cleaned up.

  • Additional configuration items (which applies to users of advanced features)

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.