wade huang wade huang - 27 days ago 30
Python Question

Amazon S3 boto - how to delete folder?

I have create a folder in s3, named "test" and I push "test_1.jpg", "test_2.jpg" into "test".

Now I want to use boto to delete folder "test".

What should I do?

Answer

There are no folders in S3. Instead, the keys form a flat namespace. However a key with slashes in its name shows specially in some programs, including the AWS console (see for example [Amazon S3 boto - how to create a folder?.

Instead of deleting "a directory", you can (and have to) list files by prefix and delete:

for key in bucket.list(prefix='your/directory/'):
    key.delete()

For more information, see S3 boto list keys sometimes returns directory key.

Comments