All Products
Search
Document Center

Object Storage Service:rm

Last Updated:Oct 08, 2023

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

Usage notes

This topic provides sample command lines that are based on the 64-bit Linux system. For other systems, replace ./ossutil64 in the commands with the specific 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 and options that you can configure to run the rm command.

    Item

    Description

    bucketname

    The name of the bucket that you want to delete or that stores the resources you want to delete.

    prefix

    The resources in the bucket whose names contain the specified prefix, such as directories or objects.

    -r, --recursive

    If you specify this option, ossutil deletes all objects whose names contain the specified prefix from the bucket. If you do not specify this option, ossutil deletes only specified objects.

    -b, --bucket

    Specifies that the command is run to delete the bucket. This option is required only when you delete a bucket.

    -m, --multipart

    Specifies that the operation is performed on the data created by incomplete multipart upload tasks in the bucket.

    -a, --all-type

    Specifies that the operation is performed on the objects whose names contain the specified prefix and data created by incomplete multipart upload tasks in the bucket.

    -f, --force

    Specifies the operation to forcefully perform. The operation is performed without a prompt for confirmation.

    --include

    Specifies that the command applies to all objects that meet specified conditions.

    For more information, see View options.

    --exclude

    Specifies that the command applies to all objects that do not meet specified conditions.

    For more information, see View options.

    --version-id

    The version ID of the object that you want to delete. This option applies only when the versioning status of the bucket is enabled or suspended.

    --all-versions

    Specifies all versions of objects. This option applies only when the versioning status of the bucket is enabled or suspended. You can specify only one of the --version-id and --all-versions options in a rm command.

    --payer

    The payer of the request. If you want the requester who accesses the resources in the specified path to pay for the traffic and request fees, set this option to requester.

    --encoding-type

    The method used to encode the prefix that follows oss://bucket_name. Valid value: url. If you do not specify this option, the prefix is not encoded.

    Delete objects

    Warning

    Objects cannot be restored after they are deleted. Exercise caution when you perform this operation.

    If you use the rm command with or without the -r option to delete one or more objects, the ListObjects (GetBucket) and DeleteObject operations are called. In this case, you are charged for sending PUT requests. For more information, see API operation calling fees.

    Warning
    • If you include the --include and --exclude options in a rm command to delete objects, accidental deletions may occur due to improper condition configurations. We recommend that you exercise caution when you include both the options in a rm command.

    • You can specify more than one condition in the --include option and the --exclude option. All conditions are applied from left to right to match objects. If you specify more than one condition, we recommend that you specify the --include option before the --exclude option.

    • 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 a 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 obtain all versions of an object, see ls.

      • 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
    • Sample response

      A success response includes the number of deleted objects and the time used to complete the deletion operation:

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

    Delete parts

    If you use the rm command with multiple options to delete parts, the ListMultipartUploads, ListParts, and AbortMultipartUpload operations are called. In this case, you are charged for sending GET requests when the ListMultipartUploads and ListParts operations are called and for sending PUT requests when the AbortMultipartUpload operation is called. For more information, see API operation calling fees.

    • Examples

      • Specify the -m option in the command to delete parts that are generated by 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 options 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 options 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
    • Sample response

      A success response includes the number of deleted objects, the number of multipart upload tasks by which the deleted parts are generated, and the time used to complete the deletion operation:

      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 success response includes the name of the bucket deleted 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. We recommend that you exercise caution when you delete a bucket that contains data.

      ./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 success response includes 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 complete the deletion operation:

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

    Common options

    If you use ossutil to switch to a bucket that is located in another region, add the -e option to the command to specify the endpoint of the region in which the specified bucket is located. If you use ossutil to switch to a bucket that belongs to another Alibaba Cloud account, you can add the -i option to the command to specify the AccessKey ID of the specified account, and add the -k option to the command to specify 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 belongs to another Alibaba Cloud account:

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

    For more information about other common options that you can use for the sync command, see View options.