Skip to content

Instantly share code, notes, and snippets.

@sdarwin
Created September 15, 2023 15:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sdarwin/dcb4afc68f0952ded62d864a6f720ccb to your computer and use it in GitHub Desktop.
Save sdarwin/dcb4afc68f0952ded62d864a6f720ccb to your computer and use it in GitHub Desktop.
delete-s3-directory.sh
#!/bin/bash
# Recursively delete an entire directory in S3, including versions and deletemarkers.
# Modify these variables:
BUCKET="_bucket_"
PROFILE="_profile_"
PREFIX="_directory_prefix_"
# Delete Object Versions:
for i in $(seq 1 1000); do
echo "Objects, Seq $i"
aws s3api list-object-versions --max-items 999 --profile $PROFILE \
--bucket $BUCKET \
--output=json \
--query='{Objects: Versions[].{Key:Key,VersionId:VersionId}}' --prefix $PREFIX > output.json
aws s3api delete-objects --profile $PROFILE --bucket $BUCKET --delete file://output.json >> results.txt
if [ "$?" = "0" ]; then
true
else
echo "aws s3api delete-objects FAILED. It may have processed all results and therefore completed."
break
fi
done
# Delete the Delete Markers:
for i in $(seq 1 1000); do
echo "Delete Markers, Seq $i"
aws s3api list-object-versions --max-items 999 --profile $PROFILE \
--bucket $BUCKET \
--output=json \
--query='{Objects: DeleteMarkers[].{Key:Key,VersionId:VersionId}}' --prefix $PREFIX > output.tmp
lines=$(wc -l output.tmp | cut -d" " -f1)
echo "$lines number of lines"
if [ $lines -gt 3997 ]; then
head -n 3997 output.tmp > output.json
echo " } ] }" >> output.json
else
head -n 3997 output.tmp > output.json
fi
aws s3api delete-objects --profile $PROFILE --bucket $BUCKET --delete file://output.json >> results.txt
if [ "$?" = "0" ]; then
true
else
echo "aws s3api delete-objects FAILED. It may have processed all results and therefore completed."
break
fi
done
@sdarwin
Copy link
Author

sdarwin commented Sep 15, 2023

version 1 is a quick hack. If someone has suggestions, without overly complicating the script, they can be considered.

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