ahkam
ahkam

Reputation: 767

How to delete empty sub folders from s3 buckets?

I'm performing 2 loops in my code. Below is the pseudo code.

get the subfolder list in the bucket
Loop through every subfolders
    loop through every objects in a subfolder
        read the objects
        delete the objects (So at the end the subfolder will be empty)
    get the subfolder list again (asuming the empty subfolder also will be deleted and new subfolders can be created by someone)

But as the result i'm getting an infinite loop since the subfolders are still there in the bucket. So i was finding a solution to delete the subfolder after the deletion of every objects in it. But I couldn't find a solution for this. Please suggest your idea

Below is the s3 folder structure

├── bucket
    └── folder-1
    └── folder-2
    └── folder-3

Upvotes: 2

Views: 1436

Answers (1)

John Rotenstein
John Rotenstein

Reputation: 270144

Folders do not actually exist in Amazon S3. Instead, the Key (filename) of each object consists of the full path of the object.

Therefore, you merely need to loop through each object for a given Prefix and delete the objects.

Here's an example in Python:

import boto3

s3_resource = boto3.resource('s3')

for object in s3_resource.Bucket('bucket-name').objects.filter(Prefix='folder-name/'):
    print('Deleting:', object.key)
    object.delete()

This will also delete objects within subfolders, since they have the same Prefix (and folders do not actually exist).

Upvotes: 5

Related Questions