Skip to content

Instantly share code, notes, and snippets.

@acg
Last active August 25, 2021 23:17
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 acg/a6a0a8b8c65bff647ef104b6ff0b2749 to your computer and use it in GitHub Desktop.
Save acg/a6a0a8b8c65bff647ef104b6ff0b2749 to your computer and use it in GitHub Desktop.
Delete all old object versions in an S3 bucket. Be extremely careful!
#!/bin/sh
set -e
BUCKET="$1" ; shift
BATCH_SIZE=100
aws s3api list-object-versions --bucket "$BUCKET" |
jq '[.Versions[] | select(.IsLatest == false) | {Key, VersionId}]' |
jq -c "_nwise($BATCH_SIZE) | {Objects:., Quiet:false}" |
tr '\n' '\0' |
xargs -0 -n1 aws s3api delete-objects --bucket "$BUCKET" --delete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment