Created
February 5, 2026 01:32
-
-
Save adgnabro/68ce49a91b814ccba9d273fa6cf182d2 to your computer and use it in GitHub Desktop.
aws s3api list-object-versions + jq + aws s3api delete-objects
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
| 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