Created
August 1, 2016 20:01
-
-
Save manojpandey/d4002120121fc9ceeff4fb41250767fe 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' | |
# add credentials here | |
akid = "" | |
secret_key = "" | |
Aws.config.update({ | |
region: 'us-east-1', | |
credentials: Aws::Credentials.new(akid, secret_key) | |
}) | |
glacier = Aws::Glacier::Client.new(region: 'us-east-1') | |
vault_id = "vault-ID-name-goes-here" | |
begin | |
jobs = glacier.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 = glacierinitiate_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.get_job_output( | |
:account_id => '-', | |
:vault_name => vault_id, | |
:job_id => succeeded_inventory_job[:job_id] | |
).body.read())['ArchiveList'] | |
puts "We have #{archives.length} archives to delete." | |
archives.each_with_index do |a, i| | |
glacier.delete_archive( | |
:account_id => '-', | |
:vault_name => vault_id, | |
:archive_id => a['ArchiveId'] | |
) | |
puts "[OK] #{i}/#{archives.length}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment