Discover how to aws s3 remove folder and aws s3 remove all files in folder using efficient CLI commands. This guide covers recursive deletions, syncing methods, and directory cleanup for optimal S3 storage management.
Quick Navigation:
Delete an S3 directory with numerous small subdirectories
I have an s3 directory that contains thousands of small subdirectories and I want to delete the entire directory. I have tried deleting the directory via the AWS CLI and AWS Console , but neither method works and the directory still exists.
- Question from repost.aws
You can store and retrieve data at any time with Amazon S3 (Simple Storage Service), a scalable object storage solution. S3 lacks actual directories and folders, in contrast to conventional file systems. Rather, it makes use of a flat namespace (bucket) that can mimic a folder organization using object keys. This implies that a folder created in S3 is basically a prefix added to the key name of the item. For example, an object with the key photos/vacation/image1.jpg appears to be inside a folder named vacation within the photos folder. However, these are just prefixes in the object's key name, and there's no actual directory structure.
Your S3 buckets may eventually fill up with extraneous files and folders, raising storage expenses and possibly creating management issues. Deleting unwanted files or folders helps in:
In fact, there are multiple ways to delete folder in Amazon S3, from AWS CLI command line, GUI, to the third-party tool, just find the best way for you to clean up Amazon S3 easily.
Strong tools for managing your S3 buckets and items are available through the AWS Command Line Interface (CLI). The aws s3 rm command can be used to delete a folder and all of its contents.
Using Recursive Option to aws s3 remove folder and Subfiles. For example, to delete a folder and all its contents, input a command using the following syntax:
aws s3 rm s3://your-bucket-name/your-folder/ --recursive
This command will remove the specified folder and all objects within it.
If you have a nested folder structure, such as photos/vacation/2025/, and want to delete everything under it, the command would be:
aws s3 rm s3://your-bucket-name/photos/vacation/2025/ --recursive
AWS S3 remove all files in folder and keep folder structure: If you wish to delete all files within a folder but keep the folder structure intact, you can use the --exclude flag.
Use --dry-run: Before executing the deletion, perform a dry run to see which files will be affected.
Use --exclude and --include: To fine-tune which files to delete, use these flags to specify patterns.
For regular cleanups, consider setting up lifecycle policies in S3 to automatically delete files after a certain period.
While both commands aim to delete folders, they have subtle differences.
Interchangeable Terms and What They Actually Do
Always use the correct AWS CLI command syntax:
aws s3 rm s3://your-bucket-name/your-folder/ --recursive
This ensures proper deletion of folders and their contents.
The aws s3 sync command can be used to synchronize directories between your local machine and S3. The --delete flag ensures that files deleted locally are also removed from S3.
Syncing Local and Remote Folders with Delete Option
To sync a local directory with an S3 bucket and delete files in the bucket that are no longer present locally:
aws s3 sync /local/directory/ s3://your-bucket-name/ --delete
Pros and Cons of Using Sync for Deletion
Pros:
Cons:
Comapred with the first method, you might find it’s easier in this way, use the AWS GUI, it’s easy to remove directory in AWS S3. This will delete the bucket altogether.
1. Sign in to AWS Management Console and open to the Amazon S3 console.
2. Choose Directory buckets and select the bucket that you want to delete.
3. Click on the Empty button to empty the bucket.
4. Once the bucket is empty, you can click on Delete button to delete bucket in AWS S3.
If you need to delete folders in AWS S3 easily, you can also try the powerful multiple cloud storage manager, MultCloud.
To delete folders in AWS S3 with MultCloud, check the following steps:
1. Please create a MultCloud account and log in.
2. Click Add Cloud > Amazon S3, input the needed information to add to MultCloud.
3. Amazon S3 will be listed on the left side, choose the folder you need to delete, and choose Delete from the top or right-click menu.
4. In the pop up window, click Delete to confirm. Wait for a while, the selected Amazon S3 folders can be deleted quickly.
AWS S3 remove folder can be easily archieved with multiple ways, like AWS CLI, AWS Console, and MultCloud. Compared with these methods, you might find MultCloud and AWS Console can be easily operated to delete folders in AWS Amazon S3. Besides, you can transfer files between clouds, like moving files from Amazon S3 to Sharepoint, move files from OneDrive to OneDrive for Business, etc. So, why not give MultCloud a try now?