How to delete a folder in S3 bucket using boto3 using Python

+1 vote
How to delete a folder in S3 bucket using boto3 using Python?
Nov 30, 2018 in AWS by Nitesh
• 3,040 points
20,192 views

1 answer to this question.

0 votes

You can delete the folder by using a loop to delete all the key inside the folder and then deleting the folder.

Here is a program that will help you understand the way it works.

import boto3
s3 = boto3.resource('s3')
bucket = s3.Bucket('aniketbucketpython')
for obj in bucket.objects.filter(Prefix='aniket1/'):
    s3.Object(bucket.name,obj.key).delete()

Hope this works!

If you are a beginner and need to know more about Python, It's recommended to go for Python Certification course today.

Thanks!

answered Nov 30, 2018 by Aniket

See dmitrybelyakov's answer that accounts for pagination requirements of boto3

Hey @y2k-shubham thank you for the answer, I was not sure of the limit of 1000.

Related Questions In AWS

0 votes
1 answer

How to download the latest file in a S3 bucket using AWS CLI?

You can use the below command $ aws ...READ MORE

answered Sep 6, 2018 in AWS by Archana
• 4,150 points
12,558 views
+1 vote
1 answer
0 votes
1 answer

AWS S3 uploading hidden files by default

versioning is enabled in your bucket. docs.aws.amazon.com/AmazonS3/latest/user-guide/….... the ...READ MORE

answered Oct 4, 2018 in AWS by Priyaj
• 58,120 points
2,343 views
0 votes
1 answer

How to decrypt the encrypted S3 file using aws-encryption-cli --decrypt

Use command : aws s3 presign s3://mybucket/abc_count.png you get ...READ MORE

answered Oct 22, 2018 in AWS by Priyaj
• 58,120 points
2,951 views
0 votes
1 answer

Import my AWS credentials using python script

Using AWS Cli  Configure your IAM user then ...READ MORE

answered Nov 16, 2018 in AWS by Jino
• 5,810 points
1,616 views
0 votes
2 answers
0 votes
1 answer