Last active
May 13, 2024 20:39
-
-
Save andy-b-84/9b9df3dc9ca8f7d50cd910b23cea5e0e to your computer and use it in GitHub Desktop.
[AWS][S3] permanently delete every objects from a versioned bucket, typically before destroying the bucket, launching headless commands (make that CPU burn, Nero)
This file contains 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 | |
bucket=$1 | |
profile=$2 | |
region=$3 | |
set -e | |
echo "Removing all versions from $bucket" | |
versions=`aws --region $region --profile $profile s3api list-object-versions --bucket $bucket |jq '.Versions'` | |
markers=`aws --region $region --profile $profile s3api list-object-versions --bucket $bucket |jq '.DeleteMarkers'` | |
let count=`echo $versions |jq 'length'`-1 | |
if [ $count -gt -1 ]; then | |
echo "removing files" | |
for i in $(seq 0 $count); do | |
key=`echo $versions | jq .[$i].Key |sed -e 's/\"//g'` | |
versionId=`echo $versions | jq .[$i].VersionId |sed -e 's/\"//g'` | |
echo "aws --region $region --profile $profile s3api delete-object --bucket $bucket --key $key --version-id $versionId &" | |
aws --region $region --profile $profile s3api delete-object --bucket $bucket --key $key --version-id $versionId & | |
done | |
fi | |
let count=`echo $markers |jq 'length'`-1 | |
if [ $count -gt -1 ]; then | |
echo "removing delete markers" | |
for i in $(seq 0 $count); do | |
key=`echo $markers | jq .[$i].Key |sed -e 's/\"//g'` | |
versionId=`echo $markers | jq .[$i].VersionId |sed -e 's/\"//g'` | |
echo "aws --region $region --profile $profile s3api delete-object --bucket $bucket --key $key --version-id $versionId &" | |
aws --region $region --profile $profile s3api delete-object --bucket $bucket --key $key --version-id $versionId & | |
done | |
fi |
Nice. Thank you.
One change I made - the command execution.
From:
echo "aws --region $region --profile $profile s3api delete-object --bucket $bucket --key $key --version-id $versionId &"
aws --region $region --profile $profile s3api delete-object --bucket $bucket --key $key --version-id $versionId &
To:
cmd="aws --region $region --profile $profile s3api delete-object --bucket $bucket --key \"$key\" --version-id $versionId --no-cli-pager &"
echo $cmd
eval $cmd
This will better deal with embedded spaces in files/keys.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Based on https://gist.github.com/weavenet/f40b09847ac17dd99d16