AWS Delete Versioned S3 files

I had to delete an AWS S3 bucket with a ton of stuff in it.  One bucket with millions of files.  It was for work and my work laptop was very locked down.  I had the aws client and python.  Now I know that there are better ways but I was busy and did not have a lot of time to devote to it.  So issues came up.  I did not know the files were versioned. I kept getting ssl-warnings from aws cli and python kept throwing ssl-warnings. Finally, I would be kicked off every hour because sessions to AWS was limited to 1 hour.

I had to stop getting kicked off. So I had to renew my session before the 1 hour limitation was up.  I did this with a python script and put it in a for loop that slept for 45 minutes and then renewed my session.  You can find info on getting session tokens at AWS id credentials.

I was going to delete all files in my bucket. Easy, I thought, just do an aws cli remove bucket command.  However, I got the dreaded ssl-warnings from the aws cli command.  To prevent this put the –no-verify-ssl argument at the end of the command

aws s3 rb s3://mybucket-name --force --no-verify-ssl

The ‘–force’ removes all file and then removes the bucket.  After many hours it finished but did not  delete the bucket.  The issue I had was versioned files in the bucket.  There is a way with aws cli but it was easier to use python.  If you get python errors with unverified HTTPS request, you can  squash them with

export PYTHONWARNINGS="ignore:Unverified HTTPS request"

The script to delete versioned objects I used is


BUCKET = 'mybucket-name'

import boto3

s3 = boto3.resource('s3',verify=False)
bucket = s3.Bucket(BUCKET)

If you don’t use boto3 for AWS python development you are putting to much effort in, use boto3. I did not want the AWS ssl verified, so that is why I put int ‘verify=False’. After several more hours the versioned files were deleted and I could remove the bucket.

Tags:, ,