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 s3://bucket[/prefix] [--recursive] [--exclude "pattern"] [--include "pattern"] [--dryrun]

PARAMETERS

--recursive
    Recursively delete all objects under the specified prefix or bucket.

--exclude
    Exclude files/objects matching the glob pattern from deletion (can repeat).

--include
    Include files/objects matching the glob pattern (overrides prior excludes; must follow --exclude).

--dryrun
    Show what would be deleted without actually performing the operation.

DESCRIPTION

The aws s3 rm command is part of the AWS Command Line Interface (CLI) for managing Amazon S3 storage. It removes specified objects or all objects under a prefix from an S3 bucket.

Unlike the low-level aws s3api delete-object, this high-level command supports recursive deletion, pattern-based inclusion/exclusion, and dry-run previews, making bulk operations efficient.

Specify targets using S3 URIs like s3://my-bucket/my-key.txt. For directories or prefixes, use --recursive to delete everything matching the path. Patterns with --exclude and --include allow fine-grained control, processed left-to-right.

This command permanently deletes objects unless S3 bucket versioning is enabled, in which case delete markers are created. Always verify with --dryrun for large deletions to avoid data loss. It streams progress for large operations and integrates with AWS credentials for secure access.

CAVEATS

Deletions are irreversible without versioning; recursive ops can delete millions of objects—use --dryrun first. No confirmation prompts. Patterns are glob-style, not regex. Fails if lacking s3:DeleteObject permissions.

EXAMPLES

Single object: aws s3 rm s3://bucket/key.txt
Recursive bucket empty: aws s3 rm s3://bucket/ --recursive
Exclude logs: aws s3 rm s3://bucket/ --recursive --exclude '*.log'

EXIT CODES

0: Success
1: General error (permissions, network, invalid args)
2: Mismatched command usage

HISTORY

Introduced in AWS CLI v2 (2019) as high-level S3 command for streamlined operations, replacing verbose low-level API calls from v1.

SEE ALSO

aws(1), aws s3 cp(1), aws s3 ls(1), rm(1)

Copied to clipboard