Skip to content

Instantly share code, notes, and snippets.

@dcolebatch
Created May 8, 2018 12:59
Show Gist options
  • Save dcolebatch/0d68c2589e1f4febe48dfcfef5a48a20 to your computer and use it in GitHub Desktop.
Save dcolebatch/0d68c2589e1f4febe48dfcfef5a48a20 to your computer and use it in GitHub Desktop.
Delete AWS Glacier vault, including all archives, from bash.
#!/bin/bash
#
# This script will delete all archives in an AWS Glacier vault, then
# delete the vault.
#
# Assumes you have already got an AWS credential profile setup.
# See `aws configure --profile`
#
# Author: david.colebatch@tidalmigrations.com
#
# CONFIGURE HERE:
VAULT_NAME=<YOUR VAULT NAME>
REGION=<us-west-2 etc.>
PROFILE=default
POLL_INTERVAL=600 # Poll the glacier job every 10 minutes
# READ BELOW FOR STEPS:
######################################
# 1. GET ARCHIVES IN VAULT:
JOB_ID=$(aws --profile $PROFILE glacier initiate-job --job-parameters '{"Type": "inventory-retrieval"}' --account-id - --region $REGION --vault-name $VAULT_NAME | jq .jobId)
echo "Got JOB_ID: $JOB_ID"
echo ""
echo "Let sleeping jobs lie, for 4-5hrs"
echo "Current time `date`"
echo "-----------------------------------"
echo "Polling every $POLL_INTERVAL seconds..."
sleep 60 # It can take a minute for the job to be registered and available to poll
######################################
# 2. GET ARCHIVE IDS
until $(aws --profile $PROFILE glacier get-job-output --job-id $JOB_ID --account-id - --region $REGION --vault-name $VAULT_NAME ./output.json | grep -v -m 1 "The job is not currently available for download"); do
echo "`date`: Sleeping for $POLL_INTERVAL seconds..."
sleep $POLL_INTERVAL
done
echo "Job complete. Continuing...."
jq -r '.ArchiveList | .[] | .ArchiveId' output.json > $VAULT_NAME_archive_ids.txt
echo "Number of archives in vault ($VAULT_NAME): `wc -l $VAULT_NAME_archive_ids.txt`"
######################################
# 3. DELETE ARCHIVES
while read archId; do
echo "deleting $archId..."
aws --profile $PROFILE glacier delete-archive --archive-id=$archId --vault-name $VAULT_NAME --account-id - --region $REGION
done <$VAULT_NAME_archive_ids.txt
######################################
# 4. DELETE VAULT
echo "Vault empty. Press CTRL+D to delete vault $VAULT_NAME, or CTRL+C to exit."
echo ""
cat
echo "Continuing...."
aws --profile $PROFILE glacier delete-vault --vault-name $VAULT_NAME --account-id - --region $REGION
echo "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment