LinuxCommandLibrary

aws-s3-rm

Remove objects from Amazon S3 buckets

TLDR

Delete a specific S3 object

$ aws s3 rm s3://[bucket_name]/[path/to/file]
copy

Preview the deletion of a specific S3 object without deleting it (dry-run)
$ aws s3 rm s3://[bucket_name]/[path/to/file] --dryrun
copy

Delete an object from a specific S3 access point
$ aws s3 rm s3://arn:aws:s3:[region]:[account_id]:[access_point]/[access_point_name]/[object_key]
copy

Remove all objects from a bucket (empty the bucket)
$ aws s3 rm s3://[bucket_name] --recursive
copy

Display help
$ aws s3 rm help
copy

SYNOPSIS

aws s3 rm [--recursive] [--dryrun] [--quiet] [--exclude '*' ] [--include '*'] [--only-show-errors] [--page-size ] [--no-progress] [--sse ] [--sse-c ] [--sse-kms-key-id ] [--acl ] [--storage-class ] [--website-redirect-location ] [--profile ] [--debug] [--endpoint-url ] [--region ] [--output ] [--query ] [--ca-bundle ] [--cli-read-timeout ] [--cli-connect-timeout ] [--verify-ssl ] [--no-verify-ssl] [--no-sign-request] [--addressing-style ] [--payload-signing-enabled] [--disable-multithreading] [--force-long-polling] [--eager-load-wait] [--max-attempts ] [--client-cert ] [--paginate ]

PARAMETERS

S3Uri
    The S3 URI of the object or prefix to remove (e.g., `s3://bucket/key` or `s3://bucket/prefix/`).

--recursive
    Recursively remove all objects under the specified prefix. This is required for deleting prefixes containing multiple objects.

--dryrun
    Performs the operation without actually deleting any objects. This is useful for previewing the effects of the command.

--exclude
    Exclude objects during recursive operations. Provide a glob-style pattern. Can be specified multiple times to specify multiple rules.

--include
    Include objects during recursive operations. Provide a glob-style pattern. Can be specified multiple times to specify multiple rules.

--only-show-errors
    Only errors are displayed, otherwise all output is suppressed.

--profile
    The AWS profile to use (if configured).

--region
    The AWS region to use.

DESCRIPTION

The `aws s3 rm` command is a powerful tool provided by the AWS Command Line Interface (CLI) for deleting objects from Amazon Simple Storage Service (S3) buckets. It allows users to remove individual objects, entire prefixes (folders), or recursively delete all content within a bucket or prefix. This command supports versioning. When versioning is enabled, you must use a different command to remove particular versions.

CAVEATS

Using `aws s3 rm --recursive` without caution can lead to unintended data loss. Always double-check the S3 URI before executing the command.
Removing versioned objects requires specific commands. This command is intended to permanently delete the object.

EXAMPLES

Remove a single object:
aws s3 rm s3://my-bucket/my-object.txt

Remove all objects under a prefix:
aws s3 rm s3://my-bucket/my-prefix/ --recursive

Dry run to preview the effect of recursive removal:
aws s3 rm s3://my-bucket/my-prefix/ --recursive --dryrun

Remove all .txt except test.txt files from s3://my-bucket/test/:
aws s3 rm s3://my-bucket/test/ --recursive --exclude "*" --include "*.txt" --exclude "test.txt"

SEE ALSO

aws s3 cp(1), aws s3 mv(1), aws s3 sync(1)

Copied to clipboard