Last active
August 1, 2016 15:12
-
-
Save bouchard/68368bc1ceab351caba8 to your computer and use it in GitHub Desktop.
Delete all archives in an Amazon Glacier Vault
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'aws-sdk' | |
require 'json' | |
glacier = AWS::Glacier.new( | |
:access_key_id => "ID_GOES_HERE", | |
:secret_access_key => "SECRET_GOES_HERE" | |
) | |
vault_id = "VAULT_ID_FROM_AWS_MANAGEMENT_CONSOLE_GOES_HERE" | |
region = "us-east-1" | |
begin | |
jobs = glacier.client.list_jobs( | |
:account_id => '-', | |
:vault_name => vault_id | |
) | |
inventory_jobs = jobs[:job_list].select do |j| | |
j[:action] == 'InventoryRetrieval' | |
end | |
puts inventory_jobs.inspect | |
succeeded_inventory_job = inventory_jobs.select do |j| | |
j[:status_code] == 'Succeeded' | |
end.last | |
if !succeeded_inventory_job | |
inventory = glacier.client.initiate_job({ | |
:account_id => '-', | |
:vault_name => vault_id, | |
:job_parameters => { | |
:type => 'inventory-retrieval' | |
} | |
}) | |
puts "Initiated Inventory Download... waiting 30 minutes" | |
sleep 1800 | |
end | |
end while !succeeded_inventory_job | |
archives = JSON.parse(glacier.client.get_job_output( | |
:account_id => '-', | |
:vault_name => vault_id, | |
:job_id => succeeded_inventory_job[:job_id] | |
).body)['ArchiveList'] | |
puts "We have #{archives.length} archives to delete." | |
archives.each_with_index do |a, i| | |
glacier.client.delete_archive( | |
:account_id => '-', | |
:vault_name => vault_id, | |
:archive_id => a['ArchiveId'] | |
) | |
puts "[OK] #{i}/#{archives.length}" | |
end |
Also, does vault_id in line 11 refer to VaultARN. I could find no reference to vault_ID anywhere in the console or the documentation: http://docs.aws.amazon.com/amazonglacier/latest/dev/api-vault-get.html
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, Thanks for writing this script. Everytime I run this after making sure the ruby aws-sdk is installed and setting my secret key/access key, I get this error:
delete_Vault.rb:6:in `
': uninitialized constant AWS (NameError)Did you mean? Aws