Skip to content

Instantly share code, notes, and snippets.

@matthiasr
Created December 14, 2020 09:18
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthiasr/8af91f1c5d36044e5be4e5a25d34552d to your computer and use it in GitHub Desktop.
Save matthiasr/8af91f1c5d36044e5be4e5a25d34552d to your computer and use it in GitHub Desktop.
Delete everything in an S3 bucket (in a hurry)
# I needed to delete ~300k objects from an S3 bucket in a hurry. The better and cheaper solution is to use a lifecycle rule, but those can take a day or two to take effect.
bucket="i-want-to-lose-all-my-data"
mkdir -p deletes
# 1. List all the objects in the bucket, transform them 1000 at a time into request objects for DeleteObjects, write each to a separate file
aws s3api list-objects-v2 --bucket "${bucket}" \
| jq -c '.Contents | _nwise(1000) | map({ Key: .Key }) | { Objects: ., Quiet: true }' \
| awk '{ f = "deletes/" NR ".json"; print $0 > f; close(f) }'
# 2. delete everything
exit 1 # remove this line if you really mean it
ls -1 deletes/*.json \
| sort -n \
| gxargs -n 1 -P 10 -I % aws s3api delete-objects --bucket "${bucket}" --delete file://%
@matthiasr
Copy link
Author

On macOS, the built-in xargs does not have -P. Install gxargs with brew install findutils. On Linux, use the regular xargs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment