Skip to content
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

Add s3 empty-and-delete-bucket command #404

Merged
merged 1 commit into from
Feb 6, 2025
Merged
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
129 changes: 129 additions & 0 deletions bin/s3/v2/empty-and-delete-bucket
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#!/bin/bash

# exit on failures
set -e
set -o pipefail

usage() {
echo "Usage: $(basename "$0") [OPTIONS]" 1>&2
echo " -h - help"
echo " -i <infrastructure> - infrastructure name"
echo " -e <environment> - environment name (e.g. 'staging' or 'prod')"
echo " -b <bucket_name> - bucket name (optional, by default goes through all s3 buckets)"
exit 1
}

# if there are no arguments passed exit with usage
if [ $# -eq 0 ]
then
usage
fi

while getopts "i:e:b:h" opt; do
case $opt in
i)
INFRASTRUCTURE_NAME=$OPTARG
;;
e)
ENVIRONMENT=$OPTARG
;;
b)
BUCKET_NAME=$OPTARG
;;
h)
usage
;;
*)
usage
;;
esac
done

if [[
-z "$INFRASTRUCTURE_NAME"
|| -z "$ENVIRONMENT"
]]
then
usage
fi

PROFILE="$(resolve_aws_profile -i "$INFRASTRUCTURE_NAME" -e "$ENVIRONMENT")"

if yes_no "Do you want to empty and delete the '$BUCKET_NAME' bucket in '$INFRASTRUCTURE_NAME' ('$ENVIRONMENT')?: " "y"
then
log_info -l "Emptying: $BUCKET_NAME ..." -q "$QUIET_MODE"
"$APP_ROOT/bin/dalmatian" aws-sso run-command \
-p "$PROFILE" \
s3 rm "s3://$BUCKET_NAME" \
--recursive

OBJECTS="$("$APP_ROOT/bin/dalmatian" aws-sso run-command \
-p "$PROFILE" \
s3api list-object-versions \
--bucket "$BUCKET_NAME")"

VERSIONS="$(echo "$OBJECTS" | jq '.Versions')"
NUM_VERSIONS=$(echo "$VERSIONS" | jq 'length')

if [ "$NUM_VERSIONS" != "0" ]
then
log_info -l "$NUM_VERSIONS versions to remove ..." -q "$QUIET_MODE"
while [ "$NUM_VERSIONS" -gt 0 ]
do
"$APP_ROOT/bin/dalmatian" aws-sso run-command \
-p "$PROFILE" \
s3api delete-objects \
--bucket "$BUCKET_NAME" \
--delete "$(echo "$VERSIONS" | \
jq '.[0:500]
| map({Key, VersionId}) | {Objects: ., Quiet: true}'
)"
VERSIONS="$(echo "$VERSIONS" | jq '.[500:]')"
NUM_VERSIONS="$(echo "$VERSIONS" | jq 'length')"
if [ "$NUM_VERSIONS" -gt 0 ]
then
log_info -l "Removed 500 versions, $NUM_VERSIONS remaining ..." -q "$QUIET_MODE"
else
log_info -l "All versions removed" -q "$QUIET_MODE"
fi
done
fi

OBJECTS="$("$APP_ROOT/bin/dalmatian" aws-sso run-command \
-p "$PROFILE" \
s3api list-object-versions \
--bucket "$BUCKET_NAME")"

DELETE_MARKERS="$(echo "$OBJECTS" | jq '.DeleteMarkers')"
NUM_DELETE_MARKERS=$(echo "$DELETE_MARKERS" | jq 'length')

if [ "$NUM_DELETE_MARKERS" != "0" ]
then
log_info -l "$NUM_DELETE_MARKERS delete markers to remove ..." -q "$QUIET_MODE"
while [ "$NUM_DELETE_MARKERS" -gt 0 ]
do
"$APP_ROOT/bin/dalmatian" aws-sso run-command \
-p "$PROFILE" \
s3api delete-objects \
--bucket "$BUCKET_NAME" \
--delete "$(echo "$DELETE_MARKERS" | \
jq '.[0:500]
| map({Key, VersionId}) | {Objects: ., Quiet: true}'
)"
DELETE_MARKERS="$(echo "$DELETE_MARKERS" | jq '.[500:]')"
NUM_DELETE_MARKERS="$(echo "$DELETE_MARKERS" | jq 'length')"
if [ "$NUM_DELETE_MARKERS" -gt 0 ]
then
log_info -l "Removed 500 delete markers, $NUM_DELETE_MARKERS remaining ..." -q "$QUIET_MODE"
else
log_info -l "All delete markers removed" -q "$QUIET_MODE"
fi
done
fi

log_info -l "Deleting: $BUCKET_NAME ..." -q "$QUIET_MODE"
"$APP_ROOT/bin/dalmatian" aws-sso run-command \
-p "$PROFILE" \
s3api delete-bucket \
--bucket "$BUCKET_NAME"
fi