Skip to content

Instantly share code, notes, and snippets.

@bouchard
Last active August 1, 2016 15:12
Show Gist options
  • Save bouchard/68368bc1ceab351caba8 to your computer and use it in GitHub Desktop.
Save bouchard/68368bc1ceab351caba8 to your computer and use it in GitHub Desktop.
Delete all archives in an Amazon Glacier Vault
#!/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
@mitsukuro1
Copy link

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

@mitsukuro1
Copy link

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