Created
May 26, 2025 00:35
-
-
Save juicedM3/bccbe171bd2fc896678c8aba84064120 to your computer and use it in GitHub Desktop.
Delete Glacier Archives quicker than shelling out to awscli every time.
This file contains hidden or 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
| package main | |
| import "context" | |
| import "bufio" | |
| import "fmt" | |
| import "log" | |
| import "os" | |
| import "github.com/aws/aws-sdk-go-v2/config" | |
| import "github.com/aws/aws-sdk-go-v2/service/glacier" | |
| // After you run this command | |
| // $ aws glacier get-job-output --job-id YOUR_JOB_ID --vault-name YOUR_VAULT_NAME --region YOUR_REGION ./output.json | |
| // | |
| // You need to grab the individual ArchiveIds from the output using jq or your favorite tool. | |
| // $ jq -r '.ArchiveList[].ArchiveId' ./output.json > archive-ids.json | |
| // | |
| // Or break it up into years or months depending on how many archive IDs you have. | |
| // $ jq -r '.ArchiveList[] | select(.CreationDate | contains("2024")) | .ArchiveId' ./output.json > 2024-archive-ids.json | |
| // Enter your AWS Account Id and AWS Vault Name | |
| var AWS_ACCOUNT_ID = "YOUR_ACCOUNT_ID" | |
| var AWS_VAULT_NAME = "YOUR_VAULT_NAME" | |
| // ./glacier-archive-delete 2024-archive-ids.json | |
| func main() { | |
| // Initialize a session that the SDK uses to load | |
| // credentials from the shared credentials file ~/.aws/credentials | |
| // and configuration from the shared configuration file ~/.aws/config. | |
| ctx := context.Background() | |
| sdkConfig, err := config.LoadDefaultConfig(ctx) | |
| if err != nil { | |
| fmt.Println("Couldn't load default configuration. Have you set up your AWS account?") | |
| fmt.Println(err) | |
| return | |
| } | |
| // fmt.Printf("sdkConfig %+v\n\n", sdkConfig) | |
| // Create Glacier client in default region | |
| client := glacier.NewFromConfig(sdkConfig) | |
| // fmt.Printf("client %+v\n\n", client) | |
| file, err := os.Open(os.Args[1]) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| defer file.Close() | |
| var dai glacier.DeleteArchiveInput | |
| dai.AccountId = &AWS_ACCOUNT_ID | |
| dai.VaultName = &AWS_VAULT_NAME | |
| scanner := bufio.NewScanner(file) | |
| for scanner.Scan() { | |
| line := scanner.Text() | |
| dai.ArchiveId = &line | |
| fmt.Println("Deleting ArchiveID: " + line) | |
| _, err := client.DeleteArchive(ctx, &dai) | |
| if err != nil { | |
| fmt.Println("Couldn't delete ArchiveId :" + line) | |
| fmt.Println(err) | |
| return | |
| } | |
| } | |
| if err := scanner.Err(); err != nil { | |
| log.Fatal(err) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment