AWS S3
boto3
delete folder
Python
cloud storage

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:

  1. An AWS account.
  2. AWS access and secret keys.
  3. Boto3 installed in your Python environment.

You can set up Boto3 and AWS credentials using:

bash
pip install boto3

Configure your AWS credentials by ensuring your ~/.aws/credentials file contains the right keys or set environment variables:

plaintext
[default]
aws_access_key_id = YOUR_ACCESS_KEY
aws_secret_access_key = YOUR_SECRET_KEY

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.

python
import boto3

s3_client = boto3.client('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.

python
def list_objects_in_folder(bucket_name, folder_path):
    response = s3_client.list_objects_v2(Bucket=bucket_name, Prefix=folder_path)
    return response.get('Contents', [])

Step 3: Delete the Objects

Iterate through the listed objects and delete them using the delete_objects method provided by Boto3.

python
1def delete_folder(bucket_name, folder_path):
2    objects_to_delete = list_objects_in_folder(bucket_name, folder_path)
3    delete_keys = [{'Key': obj['Key']} for obj in objects_to_delete]
4
5    # Perform the delete operation
6    response = s3_client.delete_objects(Bucket=bucket_name, Delete={'Objects': delete_keys})
7
8    print(f"Deleted: {response.get('Deleted', [])}")
9    if 'Errors' in response:
10        print(f"Errors: {response['Errors']}")

Complete Script

Here's the complete script for deleting a folder in Amazon S3:

python
1import boto3
2
3def delete_s3_folder(bucket_name, folder_path):
4    s3_client = boto3.client('s3')
5
6    # List all objects with the folder_path prefix
7    response = s3_client.list_objects_v2(Bucket=bucket_name, Prefix=folder_path)
8    objects_to_delete = response.get('Contents', [])
9
10    # Create a list of keys to delete
11    delete_keys = [{'Key': obj['Key']} for obj in objects_to_delete]
12
13    if delete_keys:
14        # Perform the delete operation
15        response = s3_client.delete_objects(Bucket=bucket_name, Delete={'Objects': delete_keys})
16        
17        print(f"Deleted: {response.get('Deleted', [])}")
18        if 'Errors' in response:
19            print(f"Errors: {response['Errors']}")
20    else:
21        print(f"No objects found with prefix: {folder_path}")
22
23# Example usage
24delete_s3_folder('YOUR_BUCKET_NAME', 'path/to/folder/')

Key Points

TaskCode/MethodDescription
Initialize Boto3 clientboto3.client('s3')Set up the S3 client for interaction.
List objects in folders3_client.list_objects_v2(Bucket=bucket_name, Prefix=prefix)Retrieve objects with the specified path prefix.
Format delete requestdelete_keys = [{'Key': obj['Key']}, ...]Format object keys for batch deletion.
Delete objectss3_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.


Course illustration
Course illustration

All Rights Reserved.