Skip to content

Instantly share code, notes, and snippets.

@gwsu2008
Forked from vsx-gh/s3_full_delete.py
Created October 9, 2021 22:56
Show Gist options
  • Save gwsu2008/653a1d5e8a675b083e3ad86676273183 to your computer and use it in GitHub Desktop.
Save gwsu2008/653a1d5e8a675b083e3ad86676273183 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