Permanently deleting objects from a s3 bucket that has versioning enabled – not so straight forward

Do NOT use this procedure if you have files in your versioning enabled bucket that you want to keep.

The following procedure will permanently delete all files and versions in a bucket.

It was written to be run from Linux/Bash and it assume python3 is installed, and the boto3 package is installed

remove-all-versions.bash

#! /bin/bash
export AWS_ACCESS_KEY_ID=
export AWS_SECRET_ACCESS_KEY=
python3 remove-all-versions.py

Make the bash script executable

chmod 700 remove-all-versions.bash

remove-all-versions.py

# https://gist.github.com/weavenet/f40b09847ac17dd99d16
import boto3
session = boto3.Session()
s3 = session.resource(service_name=’s3′)
bucket = s3.Bucket(‘yourBucketName’)
bucket.object_versions.delete()
# bucket.delete()

 

Leave a Comment

Scroll to Top