LinuxCommandLibrary

aws-s3-rb

Remove Amazon S3 buckets

TLDR

Delete an empty S3 bucket

$ aws s3 rb s3://[bucket_name]
copy

Force delete an S3 bucket and its non-versioned objects (will crash if versioned objects are present)
$ aws s3 rb s3://[bucket_name] --force
copy

SYNOPSIS

aws s3 rb s3://bucket_name [--force] [--dryrun]

PARAMETERS

s3://bucket_name
    Required positional argument. S3 URI of the bucket to delete.

--force, -F
    Force deletion of non-empty buckets by recursively removing all contents first.

--dryrun
    Print actions that would be taken without performing deletions.

--debug
    Display debugging information.

--no-paginate
    Disable automatic pagination.

DESCRIPTION

aws s3 rb is a high-level command in the AWS CLI for deleting Amazon S3 buckets. It removes the specified bucket, but only if it is empty by default. Use --force to delete all objects, including versions, before removing the bucket itself. This makes it ideal for cleaning up entire buckets programmatically.

Key features include support for --dryrun to simulate deletions without action, helping prevent accidents. The command processes S3 paths starting with s3:// and handles large buckets efficiently in AWS CLI v2.

Permissions required: s3:DeleteBucket, s3:DeleteObject, and s3:DeleteObjectVersion if versioning is enabled. Deletions are permanent unless MFA Delete or versioning with retention is configured. Always verify bucket names to avoid data loss.

Common use cases: Dev/test cleanup, CI/CD pipelines, and bulk bucket removal. It outperforms low-level API calls for recursive operations. Output shows progress for large buckets.

CAVEATS

Irreversible data loss; no undo without backups or versioning. Fails on buckets with locks or incomplete multipart uploads. Requires full delete permissions. Use --dryrun first.

EXAMPLES

aws s3 rb s3://my-empty-bucket # Delete empty bucket
aws s3 rb s3://my-bucket --force # Delete bucket + contents
aws s3 rb s3://my-bucket --dryrun --force # Preview deletions

EXIT CODES

0: Success
1: General error (permissions, non-empty without --force, etc.)
2: Misuse of options or invalid syntax.

HISTORY

Introduced in AWS CLI v1.7.0 (2014). Improved in v2.0+ (2020) with faster multi-threaded recursion and better error handling for massive buckets.

SEE ALSO

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

Copied to clipboard