Last active
November 10, 2021 18:44
-
-
Save res0nat0r/74ce7e78cd5994f55372897611f23938 to your computer and use it in GitHub Desktop.
Delete all non latest s3 objects
This file contains 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
#!/usr/bin/env python | |
import json | |
import sys | |
# https://docs.aws.amazon.com/cli/latest/reference/s3api/delete-objects.html | |
# USAGE: | |
# aws s3api list-object-versions --bucket > versions.json | |
# ./delete-old-versions.py ./versions.json > delete.json | |
# aws s3api delete-objects --bucket bucket --delete file://delete.json | |
out = { "Objects" : [], "Quiet": False } | |
with open(sys.argv[1]) as f: | |
data = json.load(f) | |
for item in data['Versions']: | |
if item['IsLatest'] == False: | |
out['Objects'].append({"Key": item['Key'], "VersionId": item['VersionId']}) | |
print(json.dumps(out)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment