AWS CLI
S3 bucket
file deletion
cloud storage management
Amazon Web Services

How to delete multiple files in S3 bucket with AWS CLI

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Deleting multiple objects from Amazon S3 with the AWS CLI is usually a matter of choosing the right command shape for the scope of deletion. Sometimes you want to remove everything under a prefix, and sometimes you want to delete a specific list of keys without touching anything else.

Core Sections

Delete by prefix with aws s3 rm --recursive

If the objects are grouped under a common prefix, the simplest command is a recursive remove.

bash
aws s3 rm s3://my-bucket/logs/2026/03/ --recursive

This walks the prefix and deletes every object underneath it. It is the right tool when the keys already form a clean directory-like grouping.

Before running it, preview the target keys with:

bash
aws s3 ls s3://my-bucket/logs/2026/03/ --recursive

That check matters because S3 prefixes are just key-name prefixes, not real folders. A sloppy prefix can match more objects than you intended.

Delete a specific list of objects with s3api delete-objects

If the files are not neatly grouped under one prefix, use the lower-level s3api delete-objects command. It accepts a JSON payload containing the exact keys to remove.

Create a file such as delete.json:

json
1{
2  "Objects": [
3    { "Key": "reports/january.csv" },
4    { "Key": "reports/february.csv" },
5    { "Key": "archive/old-data.zip" }
6  ]
7}

Then run:

bash
aws s3api delete-objects --bucket my-bucket --delete file://delete.json

This is safer when you need precise control and do not want a prefix-based delete to remove neighboring objects.

Use filters carefully with recursive deletes

The higher-level aws s3 rm command also supports --exclude and --include, which is useful when you want to delete only some file types.

bash
1aws s3 rm s3://my-bucket/uploads/ \
2  --recursive \
3  --exclude "*" \
4  --include "*.tmp"

That example deletes only .tmp files under the prefix. This pattern is powerful, but it is also easy to misuse if the include and exclude logic is not tested first.

Dry-run style verification matters

The AWS CLI supports --dryrun for many high-level S3 commands, including recursive removal. Use it before a broad delete.

bash
aws s3 rm s3://my-bucket/logs/2026/03/ --recursive --dryrun

This shows what would be deleted without actually removing anything. For large or production buckets, this should be routine rather than optional.

Permissions, versioning, and irreversible effects

Bulk deletion depends on IAM permissions. The caller usually needs s3:DeleteObject, and if versioned objects are involved, you may also need version-specific handling.

Also remember:

  • deleting in S3 is immediate from the CLI’s point of view
  • versioned buckets behave differently from unversioned ones
  • deleting the current version in a versioned bucket may create a delete marker instead of removing all history

That means the exact effect depends on the bucket configuration, not only on the command syntax.

Choose the command based on the deletion pattern

A good rule is:

  • use aws s3 rm --recursive when the delete target is a whole prefix
  • use aws s3api delete-objects when you have a specific set of keys
  • use filters only after a dry run proves the selection is correct

The lower-level API command is more verbose, but it is often the safer choice for surgical deletions.

Common Pitfalls

  • Treating an S3 prefix like a real folder can lead to broader recursive deletes than intended.
  • Skipping --dryrun on a recursive removal makes it much easier to delete the wrong keys.
  • Forgetting about bucket versioning can cause confusion when objects appear to persist through delete markers or prior versions.
  • Using delete-objects without carefully constructing the JSON payload can silently target the wrong object keys.
  • Running bulk deletions without checking IAM permissions first can waste time debugging access errors that are unrelated to command syntax.

Summary

  • Use aws s3 rm --recursive for prefix-based bulk deletion.
  • Use aws s3api delete-objects for exact, key-by-key deletion.
  • Validate targets first with aws s3 ls or --dryrun.
  • Be aware of IAM permissions and S3 versioning behavior before deleting.
  • Choose the most precise command that matches the actual deletion pattern.

Course illustration
Course illustration

All Rights Reserved.