Skip to content

Instantly share code, notes, and snippets.

@res0nat0r
Last active November 10, 2021 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save res0nat0r/74ce7e78cd5994f55372897611f23938 to your computer and use it in GitHub Desktop.
Save res0nat0r/74ce7e78cd5994f55372897611f23938 to your computer and use it in GitHub Desktop.
Delete all non latest s3 objects
#!/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