Skip to content

Instantly share code, notes, and snippets.

@ThomasDalla
Last active May 13, 2022 00:49
Show Gist options
  • Save ThomasDalla/d00f867feb70651668ad7d762546bbad to your computer and use it in GitHub Desktop.
Save ThomasDalla/d00f867feb70651668ad7d762546bbad to your computer and use it in GitHub Desktop.
#!/bin/sh
# Small script to delete all archives in the given vault
# Based on https://docs.aws.amazon.com/amazonglacier/latest/dev/deleting-an-archive-using-cli.html
# Setup: account-id and vault-name
account=${1:-882328332680}
vault=${2:-DS216J_001132719CB4_1}
# Start the job to retrieve archive ids
echo "`date` | Initiating job for account $account vault $vault ..."
jobId=`aws glacier initiate-job --vault-name $vault --account-id $account --job-parameters "{\"Type\":\"inventory-retrieval\"}" | jq -r '.jobId'`
# Wait until completed:
echo "`date` | Waiting until job ${jobId} completes..."
completed=false
iteration_no=0
until $completed
do
((iteration_no++))
d=`date`
printf "$d | $iteration_no | Checking job ${jobId}... "
jobStatus=`aws glacier describe-job --vault-name $vault --account-id $account --job-id "'$jobId'"`
completed=`echo $jobStatus | jq -r '.Completed'`
statusCode=`echo $jobStatus | jq -r '.StatusCode'`
printf "${statusCode} -> "
if $completed ; then echo 'Completed!' ; else echo "Not completed (${completed}). Sleeping 30s..." ; sleep 30 ; fi
done
# Save the job output
echo "Downloading job output into ${vault}.json..."
aws glacier get-job-output --vault-name $vault --account-id $account --job-id "'$jobId'" ${vault}.json | jq
# Delete each archive
archiveIds=($(jq -r '.ArchiveList | .[].ArchiveId' ${vault}.json | tr -d '[]," '))
archivesNb=${#archiveIds[@]}
echo "`date` | Found $archivesNb archives for vault $vault"
ii=0
for archiveId in "${archiveIds[@]}"
do :
((ii++))
echo "`date` | $ii / $archivesNb | Deleting ${archiveId}"
aws glacier delete-archive --vault-name $vault --account-id $account --archive-id "'$archiveId'"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment