Amazon S3 boto - how to delete folder?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Overview
Amazon S3 (Simple Storage Service) is one of AWS's most popular services, offering virtually unlimited storage space that is secure, durable, and scalable. It is an object storage service that is used to store and retrieve any amount of data from any source on the web. When using S3, objects are stored in buckets, and these objects can be organized using folders, although in reality, S3 is a flat storage system where folders are a logical construct.
Boto3 is the official AWS SDK for Python, and it provides an intuitive, Pythonic interface to AWS services, including S3. While working with S3 and Boto3, one common task is to manage folders and objects—such as deleting a folder. This article covers how to delete a folder in Amazon S3 using Boto3.
Understanding S3 Folders
In Amazon S3, folders don't physically exist; they're represented as a part of the object's key. For example, if you have an object with a key of folder1/folder2/item.txt, folder1/folder2/ is the equivalent of the folder path. Therefore, deleting a folder in S3 involves deleting all objects with keys that start with the folder's path.
Prerequisites
To interact with S3 using Boto3, you need to have:
- An AWS account.
- AWS access and secret keys.
- Boto3 installed in your Python environment.
You can set up Boto3 and AWS credentials using:
Configure your AWS credentials by ensuring your ~/.aws/credentials file contains the right keys or set environment variables:
Deleting a Folder Using Boto3
To delete a folder in Amazon S3 using Boto3, you iterate over all the objects with keys that start with the folder's path and delete each one. Here's a step-by-step guide.
Step 1: Initialize Boto3 Client
Initialize the Boto3 S3 client or resource to start interacting with S3.
Step 2: List Objects in the Folder
Using the list_objects_v2 method, list all the objects under the desired folder prefix. Replace YOUR_BUCKET_NAME and FOLDER_PATH/ with your bucket's name and the folder's prefix, respectively.
Step 3: Delete the Objects
Iterate through the listed objects and delete them using the delete_objects method provided by Boto3.
Complete Script
Here's the complete script for deleting a folder in Amazon S3:
Key Points
| Task | Code/Method | Description |
| Initialize Boto3 client | boto3.client('s3') | Set up the S3 client for interaction. |
| List objects in folder | s3_client.list_objects_v2(Bucket=bucket_name, Prefix=prefix) | Retrieve objects with the specified path prefix. |
| Format delete request | delete_keys = [{'Key': obj['Key']}, ...] | Format object keys for batch deletion. |
| Delete objects | s3_client.delete_objects(Bucket=bucket_name, Delete={'Objects': delete_keys}) | Delete the listed objects in a batch operation. |
Conclusion
Deleting a folder in Amazon S3 using Boto3 involves understanding the concept of how folders work in S3. As S3 is inherently a flat structure without folders, deleting a folder requires deletion of all the objects with keys beginning with the specified folder path. With Boto3, you can efficiently handle this task by listing the objects with the particular prefix and deleting them in a single batch operation. This method ensures you efficiently manage S3 storage without leaving orphaned data that could incur costs.

