How to delete files from S3 bucket with AWS CLI

Reading Time: 2 minutes

Instead of deleting the whole S3 bucket, we may choose to just delete objects from S3 bucket or just empty it. Here, we will discuss various method to delete files or objects from S3 bucket with AWS CLI.

Before we go into the details, please ensure that you have the latest version of AWS CLI installed by referring to AWS documentation. Also, we will be using s3 commands here and you may refer to here to illustrate the differences between s3 and s3api commands.

Types of deletion

  • Single File Deletion
  • Multiple Files Deletion
  • Folder Deletion
  • Emptying Bucket
  • Emptying Folder

Single File Deletion

Removing of single file is straight forward where we just need to use rm command to indicate removing.

# Delete a single file 'test-file.txt'
aws s3 rm s3://my-bucket/test-file.txt

Multiple Files Deletion

To delete multiple files, we can use --include and --exclude multiple times to help with deleting a few files. Let’s look at the below for a few examples below.

# Delete 'test-file1.txt' and 'test-file2.txt' while the rest of the files stay
aws s3 rm s3://my-bucket/my-folder/ --recursive --exclude "*" --include "test-file1.txt" --include "test-file2.txt"

# Delete 'test-file1.txt' and 'test-file2.txt' with prefix folder 'my-folder' while the rest of the files stay
aws s3 rm s3://my-bucket/my-folder/ --recursive --exclude "*" --include "my-folder/test-file1.txt" --include "my-folder/test-file2.txt"

This works where --exclude indicates to the command that we want to keep everything and --include indicate to the command to have delete them.

Folder Deletion

Since the folder that we are deleting have objects or sub directory inside, we need indicate --recursive so that it will remove things that are inside

# Delete a single folder 'my-folder' recursively
aws s3 rm s3://my-bucket/my-folder --recursive

Emptying Bucket

In order to empty the bucket without delete it, we just need to use --recursive on rm command.

# Delete everything in my-bucket
aws s3 rm s3://my-bucket --recursive

Emptying Folder

Since we will be deleting everything inside the folder, it is similar to deleting multiple files without the usage of --exclude and --include.

# Delete everything in my-folder
aws s3 rm s3://my-bucket/my-folder --recursive


We have discuss the various methods of deleting files and objects from S3 above. Depends on the use case, there will be one that will help to reduce the complexity to delete.

Show Comments

No Responses Yet

Leave a Reply