Amazon Glacier: Delete Archives Listed in a Vault Inventory
See more Amazon Glacier Examples
For existing Amazon Glacier vault customers. The standalone vault service is closed to new customers. These examples use its REST API version 2012-06-01, not the S3 bucket APIs used by S3 Glacier storage classes. See AWS service guidance.
Permanently deletes archives when dryRun is false. Defaults to a local preview, which still downloads the inventory. Requires an unfiltered, complete CSV inventory job. Stop concurrent uploads and review the snapshot before live deletion. The inventory can omit recent uploads and still list recently deleted archives. The loop stops on the first failed deletion; earlier successful deletions are not rolled back.
There is no batch-delete API here: each archive uses DeleteArchive. Only HTTP 204 counts as success. This procedure does not prove the vault is empty; retrieve a refreshed inventory and satisfy DeleteVault requirements separately.
Uses AWS Signature Version 4 with service glacier. Set the region once; the example builds the commercial AWS endpoint. Other partitions require the appropriate endpoint suffix. /-/ selects the signing account; change the vault name and IDs to your own resources. Load private qa_data/aws/glacier.json with string fields access_key, secret_key, and session_token for temporary credentials. Prefer short-lived role credentials. This sample does not discover profiles or refresh credentials. TLS certificate verification is enabled explicitly.
Checks transport completion, the operation-specific HTTP status, and parsed data separately. Inspect full diagnostics privately. Renew expired credentials; use bounded backoff with jitter for retryable reads. Uploads and job initiation can succeed before a connection fails, so do not blindly resubmit them. Configure IAM permissions for only the required operations and vault resources.
Chilkat Lua Downloads
local chilkat = require("chilkat")
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
-- Private configuration, not a file in public web content or source control.
-- Load current IAM credentials authorized for the Glacier operation.
local credentials = chilkat.JsonObject.new()
credentials.strict = true
local success = credentials:load_file("qa_data/aws/glacier.json")
if not success then
print("Could not load private AWS credentials.")
return
end
if (credentials:json_type_of("access_key") ~= 1) or (credentials:string_of("access_key") == "") then
print("Missing or invalid access_key.")
return
end
if (credentials:json_type_of("secret_key") ~= 1) or (credentials:string_of("secret_key") == "") then
print("Missing or invalid secret_key.")
return
end
-- Set this once to the region containing the vault.
local region = "us-west-2"
local endpoint = chilkat.StringBuilder.new()
endpoint:append("glacier.")
endpoint:append(region)
endpoint:append(".amazonaws.com")
local aws = chilkat.AuthAws.new()
aws.access_key = credentials:string_of("access_key")
aws.secret_key = credentials:string_of("secret_key")
aws.region = region
aws.service_name = "glacier"
aws.signature_version = 4
local rest = chilkat.Rest.new()
rest.connect_timeout_ms = 20000
rest.idle_timeout_ms = 60000
success = rest:set_auth_aws(aws)
if not success then
print("Could not configure AWS signing.")
return
end
-- Temporary IAM/STS credentials require their session token on every signed request.
-- Include the token in the signed query. Keep request URLs and diagnostics private.
-- Leave session_token absent or empty only for credentials that do not have one.
if credentials:has_member("session_token") then
if credentials:json_type_of("session_token") ~= 1 then
print("session_token must be a string.")
return
end
if credentials:string_of("session_token") ~= "" then
success = rest:add_query_param("X-Amz-Security-Token", credentials:string_of("session_token"))
if not success then
print("Could not set the session-token parameter.")
return
end
end
end
-- Supply a Socket to explicitly enable TLS certificate verification.
local connection = chilkat.Socket.new()
connection.require_ssl_cert_verify = true
connection.max_read_idle_ms = 60000
connection.max_send_idle_ms = 60000
success = connection:connect(endpoint:get_as_string(), 443, true, 20000)
if not success then
print("TLS connection failed; inspect diagnostics privately.")
return
end
success = rest:use_connection(connection, true)
if not success then
print("Could not attach the TLS connection.")
return
end
success = rest:add_header("x-amz-glacier-version", "2012-06-01")
if not success then
print("Could not set x-amz-glacier-version.")
return
end
-- A hyphen selects the account belonging to the signing credentials.
-- Use the original, URL-safe vault name and IDs returned by Glacier.
success = rest:add_path_param("{vault}", "chilkat")
if not success then
print("Could not set vault name.")
return
end
local body = chilkat.JsonObject.new()
local result = chilkat.JsonObject.new()
result.strict = true
local error_ = chilkat.JsonObject.new()
error_.strict = true
local requestBody = chilkat.StringBuilder.new()
local responseBody = chilkat.StringBuilder.new()
-- Preview by default. Set false only after reviewing the complete inventory.
local dryRun = true
success = rest:add_path_param("{job}", "JOB_ID")
if not success then
print("Could not set job ID.")
return
end
success = rest:full_request_no_body_sb("GET", "/-/vaults/{vault}/jobs/{job}", responseBody)
if not success then
print("Request did not complete; inspect diagnostics privately. Check state before retrying a change.")
return
end
if rest.response_status_code ~= 200 then
print("Glacier HTTP status: " .. rest.response_status_code)
success = error_:load_sb(responseBody)
if success then
if error_:json_type_of("code") == 1 then
print("AWS error code: " .. error_:string_of("code"))
end
end
print("AWS request ID: " .. rest:response_hdr_by_name("x-amzn-RequestId"))
-- Inspect full errors privately. An HTTP error is not a successful operation.
return
end
success = result:load_sb(responseBody)
if not success then
print("Expected valid JSON response.")
return
end
if result:json_type_of("Completed") ~= 5 then
print("Missing or invalid Completed.")
return
end
if result:json_type_of("StatusCode") ~= 1 then
print("Missing or invalid StatusCode.")
return
end
if result:string_of("StatusCode") == "" then
print("Empty StatusCode.")
return
end
if (not result:bool_of("Completed")) or (result:string_of("StatusCode") ~= "Succeeded") then
print("Job is not successfully complete. Check StatusCode and try later only if InProgress.")
return
end
if result:json_type_of("Action") ~= 1 then
print("Missing or invalid Action.")
return
end
if result:string_of("Action") == "" then
print("Empty Action.")
return
end
if result:string_of("Action") ~= "InventoryRetrieval" then
print("Wrong job type.")
return
end
if result:string_of("InventoryRetrievalParameters.Format") ~= "CSV" then
print("This example requires a CSV inventory job.")
return
end
if (result:has_member("InventoryRetrievalParameters.StartDate")) and (not result:is_null_of("InventoryRetrievalParameters.StartDate")) then
print("Use a complete, unfiltered inventory job for bulk deletion.")
return
end
if (result:has_member("InventoryRetrievalParameters.EndDate")) and (not result:is_null_of("InventoryRetrievalParameters.EndDate")) then
print("Use a complete, unfiltered inventory job for bulk deletion.")
return
end
if (result:has_member("InventoryRetrievalParameters.Limit")) and (not result:is_null_of("InventoryRetrievalParameters.Limit")) then
print("Use a complete, unfiltered inventory job for bulk deletion.")
return
end
if (result:has_member("InventoryRetrievalParameters.Marker")) and (not result:is_null_of("InventoryRetrievalParameters.Marker")) then
print("Use a complete, unfiltered inventory job for bulk deletion.")
return
end
if result:json_type_of("InventorySizeInBytes") ~= 2 then
print("Missing or invalid InventorySizeInBytes.")
return
end
local inventorySize = result:string_of("InventorySizeInBytes")
local files = chilkat.FileAccess.new()
local inventoryPath = "qa_output/glacier-inventory.csv"
success = rest:full_request_no_body_sb("GET", "/-/vaults/{vault}/jobs/{job}/output", responseBody)
if not success then
print("Request did not complete; inspect diagnostics privately. Check state before retrying a change.")
return
end
if rest.response_status_code ~= 200 then
print("Glacier HTTP status: " .. rest.response_status_code)
success = error_:load_sb(responseBody)
if success then
if error_:json_type_of("code") == 1 then
print("AWS error code: " .. error_:string_of("code"))
end
end
print("AWS request ID: " .. rest:response_hdr_by_name("x-amzn-RequestId"))
-- Inspect full errors privately. An HTTP error is not a successful operation.
return
end
success = responseBody:write_file(inventoryPath, "utf-8", false)
if not success then
print("Could not save CSV inventory.")
return
end
local savedSize = files:file_size_str(inventoryPath)
if (not files.last_method_success) or (savedSize ~= inventorySize) then
print("Inventory size mismatch; do not use this file.")
return
end
local csv = chilkat.Csv.new()
csv.has_column_names = true
success = csv:load_from_string(responseBody:get_as_string())
if not success then
print("Invalid CSV inventory.")
return
end
if csv:get_column_name(0) ~= "ArchiveId" then
print("Unexpected inventory CSV columns.")
return
end
if csv:get_column_name(1) ~= "ArchiveDescription" then
print("Unexpected inventory CSV columns.")
return
end
if csv:get_column_name(2) ~= "CreationDate" then
print("Unexpected inventory CSV columns.")
return
end
if csv:get_column_name(3) ~= "Size" then
print("Unexpected inventory CSV columns.")
return
end
if csv:get_column_name(4) ~= "SHA256TreeHash" then
print("Unexpected inventory CSV columns.")
return
end
-- Validate all IDs before sending the first DELETE.
local i = 0
while i < csv.num_rows do
if csv:get_cell_by_name(i, "ArchiveId") == "" then
print("Empty archive ID; nothing will be deleted.")
return
end
i = i + 1
end
i = 0
while i < csv.num_rows do
local archiveId = csv:get_cell_by_name(i, "ArchiveId")
print("Inventory archive: " .. archiveId)
if not dryRun then
success = rest:add_path_param("{archive}", archiveId)
if not success then
print("Could not set archive ID.")
return
end
success = rest:full_request_no_body_sb("DELETE", "/-/vaults/{vault}/archives/{archive}", responseBody)
if not success then
print("Request did not complete; inspect diagnostics privately. Check state before retrying a change.")
return
end
if rest.response_status_code ~= 204 then
print("Glacier HTTP status: " .. rest.response_status_code)
success = error_:load_sb(responseBody)
if success then
if error_:json_type_of("code") == 1 then
print("AWS error code: " .. error_:string_of("code"))
end
end
print("AWS request ID: " .. rest:response_hdr_by_name("x-amzn-RequestId"))
-- Inspect full errors privately. An HTTP error is not a successful operation.
return
end
end
i = i + 1
end
if dryRun then
print("Preview complete. No archives were deleted.")
else
print("Deleted the listed inventory archives. Vault emptiness is not confirmed.")
end