Skip to content

Instantly share code, notes, and snippets.

@Remiii
Last active September 1, 2024 11:05
Show Gist options
  • Save Remiii/507f500b5c4e801e4ddc to your computer and use it in GitHub Desktop.
Save Remiii/507f500b5c4e801e4ddc to your computer and use it in GitHub Desktop.
How to delete Vault (AWS Glacier) πŸ—»

How to delete Vault (AWS Glacier)

This Gist give some tips in order to remove AWS Glacier Vault with AWS CLI (ie. https://aws.amazon.com/en/cli/).

Step 1 / Retrive inventory

$ aws glacier initiate-job --job-parameters '{"Type": "inventory-retrieval"}' --vault-name YOUR_VAULT_NAME --account-id YOUR_ACCOUNT_ID --region YOUR_REGION

Wait during 3/5 hours… :-(

For the new step you need to get the JobId. When the retrive inventory is done you can get it with the following command: aws glacier list-jobs --vault-name YOUR_VAULT_NAME --region YOUR_REGION

Step 2 / Get the ArchivesIds

$ aws glacier get-job-output --job-id YOUR_JOB_ID --vault-name YOUR_VAULT_NAME --region YOUR_REGION ./output.json

See. Downloading a Vault Inventory in Amazon Glacier

You can get all the ArchiveId in the ./output.json file.

Step 3 / Delete Archives

PHP Version

<?php

$file = './output.json' ;
$accountId = 'YOUR_ACCOUNT_ID' ;
$region = 'YOUR_REGION' ;
$vaultName = 'YOUR_VAULT_NAME' ;

$string = file_get_contents ( $file ) ;
$json = json_decode($string, true ) ;
foreach ( $json [ 'ArchiveList' ] as $jsonArchives )
{
    echo 'Delete Archive: ' . $jsonArchives [ 'ArchiveId' ] . "\n" ;
    exec ( 'aws glacier delete-archive --archive-id="' . $jsonArchives [ 'ArchiveId' ] . '" --vault-name ' . $vaultName . ' --account-id ' . $accountId . ' --region ' . $region , $output ) ;
    echo $output ;
}

Python version

import json, subprocess

input_file_name = './output.json'
account_id = 'YOUR_ACCOUNT_ID'
region = 'YOUR_REGION'
vault_name = 'YOUR_VAULT_NAME'

with open(input_file_name) as f:
    json_string = f.read()

parsed_json = json.loads(json_string)
archive_list = parsed_json['ArchiveList']

for archive in archive_list:
    print("Deleting archive " + archive['ArchiveId'])
    command = "aws glacier delete-archive --archive-id='" + archive['ArchiveId'] + "' --vault-name " + vault_name + " --account-id " + account_id + " --region " + region
    subprocess.run(command, shell=True, check=True)

Bash version

A Bash version is available here: https://gist.github.com/veuncent/ac21ae8131f24d3971a621fac0d95be5

Powersheel version

$input_file_name = 'output.json'
$vault_name = 'my_vault'
# $account_id = 'AFDKFKEKF9EKALD' #not used. using - instead

$a = ConvertFrom-Json $(get-content $input_file_name)

$a.ArchiveList.archiveid | %{
write "executing: aws glacier delete-archive --archive-id=$_ --vault-name $vault_name --account-id -"
aws glacier delete-archive --archive-id=$_ --vault-name $vault_name --account-id - }

Mark: After you delete an archive, if you immediately download the vault inventory, it might include the deleted archive in the list because Amazon Glacier prepares vault inventory only about once a day.

See. Deleting an Archive in Amazon Glacier

Step 4 / Delete a Vault

$ aws glacier delete-vault --vault-name YOUR_VAULT_NAME --account-id YOUR_ACCOUNT_ID --region YOUR_REGION
@Remiii
Copy link
Author

Remiii commented Feb 20, 2020

Yep... typo sorry. It's fixed.

@wouitmil
Copy link

thanks !

@robertosanval
Copy link

Thanks for this!

@javi-g
Copy link

javi-g commented May 27, 2021

very useful, thank you!

@rikachu225
Copy link

Powershell version:

$input_file_name = 'output.json'
$vault_name = 'my_vault'
# $account_id = 'AFDKFKEKF9EKALD' #not used. using - instead

$a = ConvertFrom-Json $(get-content $input_file_name)

$a.ArchiveList.archiveid | %{
write "executing: aws glacier delete-archive --archive-id=$_ --vault-name $vault_name --account-id -"
aws glacier delete-archive --archive-id=$_ --vault-name $vault_name --account-id - }

Thank you for this, but just to confirm, we do not need to fill in our account id? Thanks again.

@Remiii
Copy link
Author

Remiii commented Nov 7, 2021

@magalage
Copy link

Thank you for the script. It helped me!

@aivus
Copy link

aivus commented Mar 6, 2024

I would recommend using https://github.com/leeroybrun/glacier-vault-remove

It's really quick in comparison with removing each archive one-by-one

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