Skip to content

adding a bucket-remove-all-objects-versions function #341

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions lib/s3-functions
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,39 @@ bucket-remove-force() {
local bucket
for bucket in $buckets; do
aws s3 rb --force "s3://${bucket}"
if [[ $? -ne 0 ]]
then
echo "Remove bucket failed, trying to delete object versions first (this may take a while)..."
bucket-remove-all-objects-versions ${bucket}
aws s3 rb --force "s3://${bucket}"
fi
done
fi
}

bucket-remove-all-objects-versions() {

local buckets=$(skim-stdin "$@")
[[ -z "$buckets" ]] && __bma_usage "bucket [bucket]" && return 1

local bucket
for bucket in $buckets; do
echo "Removing all versions from $bucket"

versions=`aws s3api list-object-versions --bucket $bucket |jq '.Versions'`
let count=`echo $versions |jq 'length'`-1

if [ $count -gt -1 ]; then
echo "removing files"
for i in $(seq 0 $count); do
echo -ne "${i} out of ${count}\r"
key=`echo $versions | jq .[$i].Key |sed -e 's/\"//g'`
versionId=`echo $versions | jq .[$i].VersionId |sed -e 's/\"//g'`
aws s3api delete-object --bucket $bucket --key '$key' --version-id $versionId > /dev/null
done
echo -ne '\n'
fi

done

}