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 Visual Basic 6.0 Downloads
' Assumes Chilkat is unlocked. See Global Unlock Sample.
Dim success As Long
success = 0
' Private configuration, not a file in public web content or source control.
' Load current IAM credentials authorized for the Glacier operation.
Dim credentials As New ChilkatJsonObject
credentials.Strict = 1
success = credentials.LoadFile("qa_data/aws/glacier.json")
If (success <> 1) Then
Debug.Print "Could not load private AWS credentials."
Exit Sub
End If
If ((credentials.JsonTypeOf("access_key") <> 1) Or (credentials.StringOf("access_key") = "")) Then
Debug.Print "Missing or invalid access_key."
Exit Sub
End If
If ((credentials.JsonTypeOf("secret_key") <> 1) Or (credentials.StringOf("secret_key") = "")) Then
Debug.Print "Missing or invalid secret_key."
Exit Sub
End If
' Set this once to the region containing the vault.
Dim region As String
region = "us-west-2"
Dim endpoint As New ChilkatStringBuilder
success = endpoint.Append("glacier.")
success = endpoint.Append(region)
success = endpoint.Append(".amazonaws.com")
Dim aws As New ChilkatAuthAws
aws.AccessKey = credentials.StringOf("access_key")
aws.SecretKey = credentials.StringOf("secret_key")
aws.Region = region
aws.ServiceName = "glacier"
aws.SignatureVersion = 4
Dim rest As New ChilkatRest
rest.ConnectTimeoutMs = 20000
rest.IdleTimeoutMs = 60000
success = rest.SetAuthAws(aws)
If (success <> 1) Then
Debug.Print "Could not configure AWS signing."
Exit Sub
End If
' 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.HasMember("session_token") = 1) Then
If (credentials.JsonTypeOf("session_token") <> 1) Then
Debug.Print "session_token must be a string."
Exit Sub
End If
If (credentials.StringOf("session_token") <> "") Then
success = rest.AddQueryParam("X-Amz-Security-Token",credentials.StringOf("session_token"))
If (success <> 1) Then
Debug.Print "Could not set the session-token parameter."
Exit Sub
End If
End If
End If
' Supply a Socket to explicitly enable TLS certificate verification.
Dim connection As New ChilkatSocket
connection.RequireSslCertVerify = 1
connection.MaxReadIdleMs = 60000
connection.MaxSendIdleMs = 60000
success = connection.Connect(endpoint.GetAsString(),443,1,20000)
If (success <> 1) Then
Debug.Print "TLS connection failed; inspect diagnostics privately."
Exit Sub
End If
success = rest.UseConnection(connection,1)
If (success <> 1) Then
Debug.Print "Could not attach the TLS connection."
Exit Sub
End If
success = rest.AddHeader("x-amz-glacier-version","2012-06-01")
If (success <> 1) Then
Debug.Print "Could not set x-amz-glacier-version."
Exit Sub
End If
' A hyphen selects the account belonging to the signing credentials.
' Use the original, URL-safe vault name and IDs returned by Glacier.
success = rest.AddPathParam("{vault}","chilkat")
If (success <> 1) Then
Debug.Print "Could not set vault name."
Exit Sub
End If
Dim body As New ChilkatJsonObject
Dim result As New ChilkatJsonObject
result.Strict = 1
Dim error As New ChilkatJsonObject
error.Strict = 1
Dim requestBody As New ChilkatStringBuilder
Dim responseBody As New ChilkatStringBuilder
' Preview by default. Set false only after reviewing the complete inventory.
Dim dryRun As Long
dryRun = 1
success = rest.AddPathParam("{job}","JOB_ID")
If (success <> 1) Then
Debug.Print "Could not set job ID."
Exit Sub
End If
success = rest.FullRequestNoBodySb("GET","/-/vaults/{vault}/jobs/{job}",responseBody)
If (success <> 1) Then
Debug.Print "Request did not complete; inspect diagnostics privately. Check state before retrying a change."
Exit Sub
End If
If (rest.ResponseStatusCode <> 200) Then
Debug.Print "Glacier HTTP status: " & rest.ResponseStatusCode
success = error.LoadSb(responseBody)
If (success = 1) Then
If (error.JsonTypeOf("code") = 1) Then
Debug.Print "AWS error code: " & error.StringOf("code")
End If
End If
Debug.Print "AWS request ID: " & rest.ResponseHdrByName("x-amzn-RequestId")
' Inspect full errors privately. An HTTP error is not a successful operation.
Exit Sub
End If
success = result.LoadSb(responseBody)
If (success <> 1) Then
Debug.Print "Expected valid JSON response."
Exit Sub
End If
If (result.JsonTypeOf("Completed") <> 5) Then
Debug.Print "Missing or invalid Completed."
Exit Sub
End If
If (result.JsonTypeOf("StatusCode") <> 1) Then
Debug.Print "Missing or invalid StatusCode."
Exit Sub
End If
If (result.StringOf("StatusCode") = "") Then
Debug.Print "Empty StatusCode."
Exit Sub
End If
If ((result.BoolOf("Completed") <> 1) Or (result.StringOf("StatusCode") <> "Succeeded")) Then
Debug.Print "Job is not successfully complete. Check StatusCode and try later only if InProgress."
Exit Sub
End If
If (result.JsonTypeOf("Action") <> 1) Then
Debug.Print "Missing or invalid Action."
Exit Sub
End If
If (result.StringOf("Action") = "") Then
Debug.Print "Empty Action."
Exit Sub
End If
If (result.StringOf("Action") <> "InventoryRetrieval") Then
Debug.Print "Wrong job type."
Exit Sub
End If
If (result.StringOf("InventoryRetrievalParameters.Format") <> "CSV") Then
Debug.Print "This example requires a CSV inventory job."
Exit Sub
End If
If ((result.HasMember("InventoryRetrievalParameters.StartDate") = 1) And (result.IsNullOf("InventoryRetrievalParameters.StartDate") <> 1)) Then
Debug.Print "Use a complete, unfiltered inventory job for bulk deletion."
Exit Sub
End If
If ((result.HasMember("InventoryRetrievalParameters.EndDate") = 1) And (result.IsNullOf("InventoryRetrievalParameters.EndDate") <> 1)) Then
Debug.Print "Use a complete, unfiltered inventory job for bulk deletion."
Exit Sub
End If
If ((result.HasMember("InventoryRetrievalParameters.Limit") = 1) And (result.IsNullOf("InventoryRetrievalParameters.Limit") <> 1)) Then
Debug.Print "Use a complete, unfiltered inventory job for bulk deletion."
Exit Sub
End If
If ((result.HasMember("InventoryRetrievalParameters.Marker") = 1) And (result.IsNullOf("InventoryRetrievalParameters.Marker") <> 1)) Then
Debug.Print "Use a complete, unfiltered inventory job for bulk deletion."
Exit Sub
End If
If (result.JsonTypeOf("InventorySizeInBytes") <> 2) Then
Debug.Print "Missing or invalid InventorySizeInBytes."
Exit Sub
End If
Dim inventorySize As String
inventorySize = result.StringOf("InventorySizeInBytes")
Dim files As New CkFileAccess
Dim inventoryPath As String
inventoryPath = "qa_output/glacier-inventory.csv"
success = rest.FullRequestNoBodySb("GET","/-/vaults/{vault}/jobs/{job}/output",responseBody)
If (success <> 1) Then
Debug.Print "Request did not complete; inspect diagnostics privately. Check state before retrying a change."
Exit Sub
End If
If (rest.ResponseStatusCode <> 200) Then
Debug.Print "Glacier HTTP status: " & rest.ResponseStatusCode
success = error.LoadSb(responseBody)
If (success = 1) Then
If (error.JsonTypeOf("code") = 1) Then
Debug.Print "AWS error code: " & error.StringOf("code")
End If
End If
Debug.Print "AWS request ID: " & rest.ResponseHdrByName("x-amzn-RequestId")
' Inspect full errors privately. An HTTP error is not a successful operation.
Exit Sub
End If
success = responseBody.WriteFile(inventoryPath,"utf-8",0)
If (success <> 1) Then
Debug.Print "Could not save CSV inventory."
Exit Sub
End If
Dim savedSize As String
savedSize = files.FileSizeStr(inventoryPath)
If ((files.LastMethodSuccess <> 1) Or (savedSize <> inventorySize)) Then
Debug.Print "Inventory size mismatch; do not use this file."
Exit Sub
End If
Dim csv As New ChilkatCsv
csv.HasColumnNames = 1
success = csv.LoadFromString(responseBody.GetAsString())
If (success <> 1) Then
Debug.Print "Invalid CSV inventory."
Exit Sub
End If
If (csv.GetColumnName(0) <> "ArchiveId") Then
Debug.Print "Unexpected inventory CSV columns."
Exit Sub
End If
If (csv.GetColumnName(1) <> "ArchiveDescription") Then
Debug.Print "Unexpected inventory CSV columns."
Exit Sub
End If
If (csv.GetColumnName(2) <> "CreationDate") Then
Debug.Print "Unexpected inventory CSV columns."
Exit Sub
End If
If (csv.GetColumnName(3) <> "Size") Then
Debug.Print "Unexpected inventory CSV columns."
Exit Sub
End If
If (csv.GetColumnName(4) <> "SHA256TreeHash") Then
Debug.Print "Unexpected inventory CSV columns."
Exit Sub
End If
' Validate all IDs before sending the first DELETE.
Dim i As Long
i = 0
Do While (i < csv.NumRows)
If (csv.GetCellByName(i,"ArchiveId") = "") Then
Debug.Print "Empty archive ID; nothing will be deleted."
Exit Sub
End If
i = i + 1
Loop
i = 0
Do While (i < csv.NumRows)
Dim archiveId As String
archiveId = csv.GetCellByName(i,"ArchiveId")
Debug.Print "Inventory archive: " & archiveId
If (dryRun <> 1) Then
success = rest.AddPathParam("{archive}",archiveId)
If (success <> 1) Then
Debug.Print "Could not set archive ID."
Exit Sub
End If
success = rest.FullRequestNoBodySb("DELETE","/-/vaults/{vault}/archives/{archive}",responseBody)
If (success <> 1) Then
Debug.Print "Request did not complete; inspect diagnostics privately. Check state before retrying a change."
Exit Sub
End If
If (rest.ResponseStatusCode <> 204) Then
Debug.Print "Glacier HTTP status: " & rest.ResponseStatusCode
success = error.LoadSb(responseBody)
If (success = 1) Then
If (error.JsonTypeOf("code") = 1) Then
Debug.Print "AWS error code: " & error.StringOf("code")
End If
End If
Debug.Print "AWS request ID: " & rest.ResponseHdrByName("x-amzn-RequestId")
' Inspect full errors privately. An HTTP error is not a successful operation.
Exit Sub
End If
End If
i = i + 1
Loop
If (dryRun = 1) Then
Debug.Print "Preview complete. No archives were deleted."
Else
Debug.Print "Deleted the listed inventory archives. Vault emptiness is not confirmed."
End If