Skip to content

Instantly share code, notes, and snippets.

@vsx-gh
Created September 22, 2017 14:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save vsx-gh/fc0a1f347f1e704da0af5e025ad65c8d to your computer and use it in GitHub Desktop.
Save vsx-gh/fc0a1f347f1e704da0af5e025ad65c8d to your computer and use it in GitHub Desktop.
Remove completely S3 objects that have delete markers
#!/usr/bin/env python3
'''
Program: s3_full_delete.py
Author: https://github.com/vsx-gh
Created: 20170920
Program finds S3 objects with delete markers and deletes all versions
of those objects.
Be very careful running this code!
'''
import boto3
from collections import defaultdict
working_bucket = '<your_bucket_name>'
id_list = defaultdict() # Holds delete markers
del_obj_list = defaultdict(list) # Holds all version IDs for objects w/delete markers
s3_client = boto3.client('s3')
bucket = boto3.resource('s3').Bucket(working_bucket)
resp = s3_client.list_object_versions(Bucket=working_bucket)
# All delete markers that are latest version, i.e., should be deleted
del_markers = {item['Key']: item['VersionId'] for item in resp['DeleteMarkers'] if item['IsLatest'] == True}
# Get all version IDs for all objects that have eligible delete markers
for item in resp['Versions']:
if item['Key'] in del_markers.keys():
del_obj_list[item['Key']].append(item['VersionId'])
# Remove old versions of object by VersionId
for del_item in del_obj_list:
print('Deleting {}....'.format(del_item))
rm_obj = bucket.Object(del_item)
for del_id in del_obj_list[del_item]:
rm_obj.delete(VersionId=del_id)
# Remove delete marker
rm_obj.delete(VersionId=del_markers[del_item])
print('-----\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment