Created
September 15, 2023 15:35
-
-
Save sdarwin/dcb4afc68f0952ded62d864a6f720ccb to your computer and use it in GitHub Desktop.
delete-s3-directory.sh
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
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
version 1 is a quick hack. If someone has suggestions, without overly complicating the script, they can be considered.