This topic describes how to run the rm command to delete objects, parts, or buckets that you no longer need to avoid unnecessary charges.

Notice Sample command lines in this topic are based on the 64-bit Linux system. For other systems, replace ./ossutil64 in the commands with the corresponding binary name. For more information, see ossutil.

Command syntax

./ossutil64 rm oss://bucketname[/prefix]
[-r, --recursive]
[-b, --bucket]
[-m, --multipart]
[-a, --all-type]
[-f, --force]
[--include <value>]
[--exclude <value>]
[--version-id <value>] 
[--all-versions]
[--payer <value>]
[--encoding-type <value>]

The following table describes the parameters that you can configure when you run this command.

Parameter Description
bucketname The name of the bucket in which the objects you want to delete is stored.
prefix The resources in the bucket whose names contain the specified prefix, such as directories or objects.
-r, --recursive If you specify this parameter, ossutil deletes all objects whose names contain the specified prefix from the bucket. If you do not specify this parameter, ossutil deletes only specified objects.
-b, --bucket Specifies the operations performed only when you delete buckets.
-m, --multipart Specifies the incomplete multipart upload tasks initiated to the bucket.
-a, --all-type Specifies the objects and incomplete multipart upload tasks initiated to the bucket.
-f, --force Specifies the command to forcibly run without prompting the user for confirmation.
--include Specifies that all objects that meet specified conditions are included.
--exclude Specifies that the command applies to all objects that do not meet specified conditions.
--version-id The ID of the version of the object you want to delete. This parameter applies only to objects in buckets for which versioning is enabled or suspended.
--all-versions Specifies all versions of objects. This parameter applies only to objects in buckets for which versioning is enabled or suspended. You can specify only one of the --version-id and --all-versions parameters in a single rm command.
--payer The payer of the traffic and request fees charged when the command is run. If you want that the requester who accesses the resources to be charged for the traffic and request fees, set this parameter to requester.
--encoding-type The method used to encode the prefix specified by the object name that follows oss://bucket_name in the full object path. Valid value: url. If this parameter is not specified, the prefix is not encoded.

Delete objects

  • Examples
    • Delete a single object

      Delete an object named exampleobject.txt from a bucket named examplebucket.

      ./ossutil64 rm oss://examplebucket/exampleobject.txt
    • Delete all objects whose names contain the "test" prefix from a bucket named examplebucket.
      ./ossutil64 rm oss://examplebucket/test -r
    • Delete all objects whose names contain the ".png" suffix from a bucket named examplebucket.
      ./ossutil64 rm oss://examplebucket  --include "*.png" -r
    • Delete objects whose names contain the string "abc" and do not contain the ".jpg" or ".txt" suffix from a bucket named examplebucket.
      ./ossutil64 rm oss://examplebucket  --include "*abc*" --exclude "*.jpg" --exclude "*.txt" -r
    • Delete the specified version of an object named exampleobject.txt from a versioned bucket named examplebucket.
      ./ossutil64 rm oss://examplebucket/exampleobject.txt --version-id  CAEQARiBgID8rumR2hYiIGUyOTAyZGY2MzU5MjQ5ZjlhYzQzZjNlYTAyZDE3****

      For more information about how to query all versions of an object, see Is.

    • Delete all versions of an object named exampleobject.txt from a versioned bucket named examplebucket.
      ./ossutil64 rm oss://examplebucket/exampleobject.txt --all-versions
    • Delete all versions of all objects from a versioned bucket named examplebucket.
      ./ossutil64 rm oss://examplebucket --all-versions -r
  • Output

    After the specified objects are deleted by running the preceding commands, a similar output is displayed, which indicates the number of deleted objects and the time used to delete the objects:

    Succeed: Total 8 objects. Removed 8 objects.
    0.106852(s) elapsed

Delete parts

  • Examples
    • Specify the -m parameter in the command to delete parts that are generated by the incomplete multipart upload tasks of an object named exampleobject.txt from a bucket named examplebucket.
      ./ossutil64 rm -m oss://examplebucket/exampleobject.txt
    • Specify the -m and -r parameters in the command to recursively delete parts that are generated by the incomplete multipart upload tasks of objects whose names contain the "test" prefix from a bucket named examplebucket.
      ./ossutil64 rm -m oss://examplebucket/test -r 
      Do you really mean to remove recursively multipart uploadIds of oss://examplebucket/test(y or N)? y 
    • Specify the -a and -r parameters in the command to recursively delete objects whose names contain the "src" prefix and parts that are generated by the incomplete multipart upload tasks of these objects from a bucket name examplebucket.
      ./ossutil64 rm  oss://examplebucket/src -a -r
      Do you really mean to remove recursively objects and multipart uploadIds of oss://examplebucket/src(y or N)? y
  • Output
    After the specified objects and parts are deleted by running the preceding commands, a similar output is displayed, which indicates the number of deleted objects, the number of multipart upload tasks by which the deleted parts are generated, and the time used to delete the objects and parts:
    Succeed: Total 1 objects, 3 uploadIds. Removed 1 objects, 3 uploadIds.
    1.922915(s) elapsed

Delete buckets

  • Delete the examplebucket bucket that contains no objects or parts.
    ./ossutil64 rm  oss://examplebucket -b
    Do you really mean to remove the Bucket: examplebucket(y or N)? y
    A similar output is displayed, which indicates the name of the deleted bucket and the time used to delete the bucket:
    Removed Bucket: examplebucket
    2.230745(s) elapsed
  • Delete a bucket named examplebucket and all objects and parts in the bucket.
    Warning If you run the following command, all data in the bucket is deleted and deleted data cannot be recovered. Exercise caution when you run this command.
    ./ossutil64 rm  oss://examplebucket -b -a -r
    Do you really mean to remove recursively objects and multipart uploadIds of oss://examplebucket(y or N)? y
    Do you really mean to remove the Bucket: examplebucket(y or N)? y

    A similar output is displayed, which indicates the number of deleted objects, the number of multipart upload tasks by which the deleted parts are generated, the name of the deleted bucket, and the time used to delete the objects, parts, and bucket:

    Succeed: Total 189 objects, 37 uploadIds. Removed 189 objects, 37 uploadIds.
    Removed Bucket: examplebucket
    9.184193(s) elapsed

Common options

To use ossutil to manage buckets that are located in different regions, you can use the -e option to use the endpoint of the specified bucket. To use ossutil to manage buckets that are owned by different Alibaba Cloud accounts, you can use the -i option to use the AccessKey ID of the specified account, and use the -k option to use the AccessKey secret of the specified account.

For example, you can run the following command to delete an object named exampletest.png from a bucket named testbucket, which is located in the China (Shanghai) region and owned by another Alibaba Cloud account:

./ossutil64 rm oss://testbucket/exampletest.png -e oss-cn-shanghai.aliyuncs.com -i LTAI4Fw2NbDUCV8zYUzA****  -k 67DLVBkH7EamOjy2W5RVAHUY9H****