Skip to content

Instantly share code, notes, and snippets.

@joariasl
Last active October 2, 2025 16:09
Show Gist options
  • Save joariasl/47d80b6f2a5df682ed337b5a54613d1c to your computer and use it in GitHub Desktop.
Save joariasl/47d80b6f2a5df682ed337b5a54613d1c to your computer and use it in GitHub Desktop.
Clean and Delete Legacy Glacier Valut
  1. Setup Env vars

    export AWS_REGION=<THE_REGION>
    GLACIER_VAULT=<glacier-vault-name>
  2. Initiate an Inventory Retrieval Job

    aws glacier initiate-job --account-id - --vault-name $GLACIER_VAULT --job-parameters '{"Type": "inventory-retrieval"}'

🕒 Wait >1day

  1. Get the Job Output After the job is completed, get the job ID from the list of jobs

    aws glacier list-jobs --account-id - --vault-name $GLACIER_VAULT
  2. Download the inventory

    aws glacier get-job-output --account-id - --vault-name $GLACIER_VAULT --job-id $(aws glacier list-jobs --account-id - --vault-name $GLACIER_VAULT | jq -r '.JobList[0].JobId') $GLACIER_VAULT.json

    or

    aws glacier get-job-output --account-id - --vault-name $GLACIER_VAULT --job-id <JOB_ID> $GLACIER_VAULT.json
  3. Delete Archives Loop through the archive IDs and delete them one by one

    while read archive_id; do
      aws glacier delete-archive --account-id - --vault-name $GLACIER_VAULT --archive-id="$archive_id"
    done < <(jq -r --stream 'select(.[0][-1]=="ArchiveId") | .[1]' $GLACIER_VAULT.json)

    or

    jq -r --stream 'select(.[0][-1]=="ArchiveId") | .[1]' $GLACIER_VAULT.json > $GLACIER_VAULT.txt
    while read archive_id; do
      aws glacier delete-archive --account-id - --vault-name $GLACIER_VAULT --archive-id="$archive_id"
    done < $GLACIER_VAULT.txt

    With output

    total=$(jq -r --stream 'select(.[0][-1]=="ArchiveId") | .[1]' $GLACIER_VAULT.json | awk 'END{print NR}'); while read -r num archive_id; do
      echo "[$num/$total] Delete $archive_id"
      aws glacier delete-archive --account-id - --vault-name $GLACIER_VAULT --archive-id="$archive_id"
    done < <(jq -r --stream 'select(.[0][-1]=="ArchiveId") | .[1]' $GLACIER_VAULT.json | cat -n)

🕒 Wait >1day

  1. Check if it's empty

    The NumberOfArchives attribute must be 0

    aws glacier describe-vault --account-id - --vault-name $GLACIER_VAULT
  2. Delete Glacier Vault

    aws glacier delete-vault --account-id - --vault-name $GLACIER_VAULT
@joariasl
Copy link
Author

joariasl commented Sep 7, 2025

Run it in parallel:

total=$(jq -r '.ArchiveList | length' $GLACIER_VAULT.json);
skip=0;
total=$total GLACIER_VAULT=$GLACIER_VAULT xargs -P "50" -n 2 sh -c '
  num="$1"; archive_id="$2"
  echo "[$num/$total] Delete $archive_id"
  aws glacier delete-archive --account-id - --vault-name $GLACIER_VAULT --archive-id="$archive_id"
' _ < <(jq -r '.ArchiveList[].ArchiveId' $GLACIER_VAULT.json | cat -n | tail -n +$skip)

Reverse order:

total=$(jq -r '.ArchiveList | length' $GLACIER_VAULT.json);
skip=0;
total=$total GLACIER_VAULT=$GLACIER_VAULT xargs -P "50" -n 2 sh -c '
  num="$1"; archive_id="$2"
  echo "[$num/$total] Delete $archive_id"
  aws glacier delete-archive --account-id - --vault-name $GLACIER_VAULT --archive-id="$archive_id"
' _ < <(jq -r '.ArchiveList[].ArchiveId' $GLACIER_VAULT.json | cat -n | head -n $(($total-$skip)) | tail -r)
@joariasl
Copy link
Author

joariasl commented Sep 26, 2025

Using stream to large JSON file:

total=$(jq -r --stream 'select(.[0][-1]=="ArchiveId") | .[1]' $GLACIER_VAULT.json | wc -l);
skip=0;
total=$total GLACIER_VAULT=$GLACIER_VAULT xargs -P "50" -n 2 sh -c '
  num="$1"; archive_id="$2"
  echo "[$num/$total] Delete $archive_id"
  aws glacier delete-archive --account-id - --vault-name $GLACIER_VAULT --archive-id="$archive_id"
' _ < <(jq -r --stream 'select(.[0][-1]=="ArchiveId") | .[1]' $GLACIER_VAULT.json | cat -n | tail -n +$skip)
@joariasl
Copy link
Author

joariasl commented Oct 1, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment