Skip to content

Instantly share code, notes, and snippets.

@abiusx
Last active August 31, 2020 19:43
Show Gist options
  • Save abiusx/7efddcac12c250b93de1d7d66fa16b35 to your computer and use it in GitHub Desktop.
Save abiusx/7efddcac12c250b93de1d7d66fa16b35 to your computer and use it in GitHub Desktop.
Delete all archives in a AWS S3 Glacier vault so that it can be deleted
#!/bin/bash
VAULT=${1:-myvault}
# Set your profile before by doing aws --profile XXX configure
PROFILE=${2:-myprofile}
REGION=${3:-us-east-1}
SLEEP_TIME=60
# Getting your account ID
ACCOUNTID=$(aws --profile $PROFILE sts get-caller-identity | jq -r ".Account")
# Creating a job for glacier inventory retrieval
JOBID=$(aws --region $REGION --profile $PROFILE glacier initiate-job --account-id $ACCOUNTID --vault-name $VAULT --job-parameters '{"Type": "inventory-retrieval"}' --output json | jq -r '.jobId')
echo "JOBID=$JOBID" # For future reference
# Wait for the job to complete (may take hours)
while :; do
RES=$(aws --region $REGION --profile $PROFILE glacier describe-job --account-id $ACCOUNTID --vault-name $VAULT --job-id "$JOBID" | jq -r ".StatusCode")
echo "$VAULT: $RES"
if [[ $RES != "InProgress" ]]; then break; fi
sleep $SLEEP_TIME;
done
echo "JOBID=$JOBID"
# Put result of job in file
aws --region $REGION --profile $PROFILE glacier get-job-output --account-id $ACCOUNTID --vault-name $VAULT --job-id "$JOBID" "${VAULT}.txt"
# Get list of archives retrieved from job
ARCHIVEIDS=$(cat "${VAULT}.txt" | jq -r ".ArchiveList[].ArchiveId")
# Delete all archives in vault
for ARCHIVEID in $ARCHIVEIDS; do
echo "Deleting $ARCHIVEID"
aws --region $REGION --profile $PROFILE glacier delete-archive --account-id $ACCOUNTID --vault-name $VAULT --archive-id "$ARCHIVEID" --output json
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment