Created
March 5, 2025 09:49
-
-
Save l13t/c4b26fb5fb88d0e18a16ac528ff7e950 to your computer and use it in GitHub Desktop.
remove_deletemarkers.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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