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
To delete a single object, you must have the
oss:DeleteObject
permission. To delete all objects in a directory, you must have theoss:DeleteObject
andoss:ListObjects
permissions. To delete a specific object version, you must have theoss:DeleteObjectVersion
permission. To delete all object versions in a bucket, you must have theoss:ListObjectVersions
andoss:DeleteObjectVersion
permissions. To delete all object versions in a directory, you must have theoss:DeleteObjectVersion
andoss:ListObjects
permissions. To delete parts of an object, you must have theoss:ListMultipartUploads
andoss:AbortMultipartUpload
permissions. To delete all objects and parts in a bucket, you must have theoss:ListMultipartUploads
,oss:AbortMultipartUpload
,oss:ListObjects
, andoss:DeleteObject
permissions. To delete a bucket, you must have theoss:DeleteBucket
permission.For ossutil 1.6.16 and later, you can directly use ossutil as the binary name in the command line. You do not need to update the binary name based on the operating system. For ossutil earlier than 1.6.16, you need to update the binary name based on the operating system. For more information, see ossutil command reference.
Command syntax
ossutil 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 in the syntax.
Parameter/Option | Description |
bucketname | The name of the bucket. |
prefix | The prefix in the names of resources, such as directories and 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 the specified objects. |
-b, --bucket | Specifies that the command deletes the bucket. This option is required only when you delete a bucket. |
-m, --multipart | Specifies that the operation is performed on the 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 | Forces the operation without requiring confirmation. |
--include | Includes all objects that meet the specified conditions. For more information, see Options --include and --exclude. |
--exclude | Excludes all objects that meet the specified conditions. For more information, see Options --include and --exclude. |
--version-id | The version ID of the object. This parameter applies only to a versioning-enabled or versioning-suspended bucket. |
--all-versions | Specifies all versions of objects. This option 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 options in a single rm command. |
--payer | The payer of the fees that are generated by the operation. If you want the requester who accesses the resources in the specified path to pay the fees that are generated by the operation, such as traffic and request fees, set this parameter to requester. |
--encoding-type | The encoding of the prefix that follows |
Delete objects
Objects cannot be recovered 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.
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.
ossutil rm oss://examplebucket/exampleobject.txt
Delete all objects whose names contain the "test" prefix from a bucket named examplebucket.
ossutil rm oss://examplebucket/test -r
Delete all objects whose names contain the ".png" suffix from a bucket named examplebucket.
ossutil 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.
ossutil rm oss://examplebucket --include "*abc*" --exclude "*.jpg" --exclude "*.txt" -r
Delete a specific version of an object named exampleobject.txt from a versioning-enabled bucket named examplebucket.
ossutil 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 versioning-enabled bucket named examplebucket.
ossutil rm oss://examplebucket/exampleobject.txt --all-versions
Delete all versions of all objects from a versioning-enabled bucket named examplebucket.
ossutil rm oss://examplebucket --all-versions -r
Sample output
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.
ossutil 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.
ossutil 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 named examplebucket.
ossutil 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.
ossutil 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.
WarningIf you run the following command, all data in the bucket is deleted and deleted data cannot be recovered. Exercise caution when you perform this operation.
ossutil 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, 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:
ossutil rm oss://testbucket/exampletest.png -e oss-cn-shanghai.aliyuncs.com -i LTAI4Fw2NbDUCV8zYUzA**** -k 67DLVBkH7EamOjy2W5RVAHUY9H****
For more information about common options, see Common options.