Quickly delete a bucket and its previous versions using the Java SDK

Updated at:

You cannot delete a bucket in Object Storage Service (OSS) if it is not empty. You must first delete all files, incomplete multipart upload fragments, existing LiveChannels, and versioning data before you can delete the bucket. This topic describes how to use the Java software development kit (SDK) for OSS to completely delete a bucket. You can delete the bucket using the provided code or by running a JAR package. The code also lets you delete only previous object versions and keep the latest version.

Detailed information

Note
  • If you perform risky operations, such as modifying an instance or data, consider the disaster recovery and fault tolerance capabilities of the instance to ensure data security.

  • If you modify the configuration or data of an instance, such as an ECS instance or an RDS instance, create a snapshot or enable a feature such as RDS log backup in advance.

  • If you have granted permissions or provided security information, such as logon credentials, on the Alibaba Cloud platform, change the information promptly.

Prerequisites

Before you delete the bucket, note the following prerequisites:

  • The code requires the following parameters:

    • endpoint: The endpoint of the storage region where the bucket is located. If you use an internal same-region endpoint, you must run the code on an ECS instance in the same region.

    • accessKeyId and accessKeySecret: The credentials must grant permissions to manage the bucket.

    • bucketName: The name of the bucket to delete.

    • action: This parameter has two valid values. `deletebucket` deletes the entire bucket. `deleteversion` deletes previous versions of objects but keeps the latest version.

    • prefix: This parameter is used when `action` is set to `deleteversion`. It specifies the prefix of the objects whose previous versions you want to delete.

  • JDK 1.7 or later. The pom.xml configuration for Maven dependencies is as follows.

    <dependency>
    <groupId>com.aliyun.oss</groupId>
    <artifactId>aliyun-sdk-oss</artifactId>
    <version>3.10.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/log4j/log4j -->
    <dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/commons-cli/commons-cli -->
    <dependency>
    <groupId>commons-cli</groupId>
    <artifactId>commons-cli</artifactId>
    <version>1.4</version>
    </dependency>

  • The runnable JAR package is available for download at ossdelete.jar.

  • The source code package is available for download at OssVersion.zip.

Solution

Warning

Data and buckets cannot be recovered after they are deleted. Proceed with caution. If the data is important, back up the data first.

After you download the JAR package to your local machine, run the command that applies to your scenario.

  • Run the following command to delete an entire bucket. The bucket may contain multipart upload fragments, versioning data, LiveChannel data, and a large amount of stored data.

    java -jar ossdelete.jar --endpoint=http://oss-XXXXXX.com --accessKeyId=XXXXXX --accessKeySecret=XXXXXX --bucketName=XXXXXX --action=deletebucket
    Note

    Replace the values of the `endpoint`, `accessKeyId`, `accessKeySecret`, and `bucketName` parameters with your actual information.

    The output is similar to the following.

  • Run the following command to delete only previous versions of objects and keep the latest version.

    java -jar ossdelete.jar --endpoint=http://oss-XXXXXX.com --accessKeyId=XXXXXX --accessKeySecret=XXXXXX --bucketName=XXXXXX --action=deleteversion --prefix=test/
    Note

    The `prefix` parameter specifies the prefix of the objects whose previous versions you want to delete. If you do not specify this parameter or leave it empty, the previous versions of all objects are deleted. Objects that have only one version are not affected.

    The output is similar to the following.

References

Applies to

  • Object Storage Service (OSS)