Skip to content

Instantly share code, notes, and snippets.

@adgnabro
Created February 5, 2026 01:32
Show Gist options
  • Select an option

  • Save adgnabro/68ce49a91b814ccba9d273fa6cf182d2 to your computer and use it in GitHub Desktop.

Select an option

Save adgnabro/68ce49a91b814ccba9d273fa6cf182d2 to your computer and use it in GitHub Desktop.
aws s3api list-object-versions + jq + aws s3api delete-objects
aws s3api list-object-versions --bucket $BUCKET > objects.json
jq '{
TotalVersions: (.Versions | length),
TotalDeleteMarkers: (.DeleteMarkers | length)
}' objects.json
jq -c '{Objects: [ (.Versions // []), (.DeleteMarkers // []) | .[] |
select(
(.Key | startswith("sub-prefix/")) and
(.Key | startswith("sub-prefix/terraform/") | not) and
(.Key | startswith("sub-prefix/data/") | not) and
((.Key | split("/") | length - 1) > 3)
) | {Key, VersionId}], Quiet: false}' objects.json > delete-request.json
jq '.Objects | length' delete-request.json
aws s3api delete-objects --bucket $BUCKET --delete file://delete-request.json
aws s3api delete-objects --bucket $BUCKET --delete \
"$(jq -c '{Objects: [ (.Versions // []), (.DeleteMarkers // []) | .[] |
select(
(.Key | startswith("sub-prefix/")) and
(.Key | startswith("sub-prefix/terraform/") | not) and
(.Key | startswith("sub-prefix/data/") | not) and
((.Key | split("/") | length - 1) > 3)
) | {Key, VersionId}], Quiet: false}' objects.json)"
aws s3api delete-objects --bucket $BUCKET --delete \
"$(jq -c '{Objects: [.Versions[] | select(.Key | startswith("logs/")) | {Key, VersionId}], Quiet: false}' objects.json)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment