Skip to content

Instantly share code, notes, and snippets.

@l13t
Created March 5, 2025 09:49
Show Gist options
  • Save l13t/c4b26fb5fb88d0e18a16ac528ff7e950 to your computer and use it in GitHub Desktop.
Save l13t/c4b26fb5fb88d0e18a16ac528ff7e950 to your computer and use it in GitHub Desktop.
remove_deletemarkers.py
import boto3
def delete_all_versions(bucket_name):
s3 = boto3.client('s3')
while True:
# List object versions (limited to 1000 per request)
response = s3.list_object_versions(Bucket=bucket_name)
objects_to_delete = []
# Collect object versions and delete markers
if 'Versions' in response:
for version in response['Versions']:
objects_to_delete.append({'Key': version['Key'], 'VersionId': version['VersionId']})
if 'DeleteMarkers' in response:
for marker in response['DeleteMarkers']:
objects_to_delete.append({'Key': marker['Key'], 'VersionId': marker['VersionId']})
# If no more objects to delete, break the loop
if not objects_to_delete:
print("No more objects or delete markers found. Cleanup complete!")
break
# Delete objects in bulk (up to 1000 at a time)
s3.delete_objects(
Bucket=bucket_name,
Delete={'Objects': objects_to_delete}
)
print(f"Deleted {len(objects_to_delete)} objects...")
print("All objects and delete markers have been permanently removed.")
# Replace with your S3 bucket name
bucket_name = "<bucket_name>"
delete_all_versions(bucket_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment