Created
August 30, 2024 01:13
-
-
Save xwipeoutx/bddfdfec9e620a5ae268ad08c6f34df3 to your computer and use it in GitHub Desktop.
Powershell script to delete github artifacts older than N days old.
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
# Variables | |
$repo = "Owner/RepoName" | |
$daysAgo = 14 | |
$twoWeeksAgo = (Get-Date).AddDays(-$daysAgo).ToString("yyyy-MM-ddTHH:mm:ssZ") | |
# Fetch all artifacts older than 2 weeks | |
$artifacts = gh api "repos/$repo/actions/artifacts" --paginate --jq ".artifacts | map(select(.created_at < `"`"$twoWeeksAgo`"`"`"))" | ConvertFrom-Json | |
# Check if there are any artifacts | |
if (-not $artifacts) { | |
Write-Output "No artifacts found that are older than ${daysAgo} days." | |
exit | |
} | |
# List artifacts | |
$artifactsList = $artifacts | ForEach-Object { "$($_.id): $($_.name) (Created at: $($_.created_at)) $([math]::floor($_.size_in_bytes / 1024 / 1024))MB" } | |
$artifactsList | ForEach-Object { Write-Output $_ } | |
# # Confirm deletion | |
$confirm = Read-Host "Do you want to delete these $($artifacts.length) artifacts from ${daysAgo} days ago? (y/N)" | |
if ($confirm -match '^(y|Y)$') { | |
$artifacts | ForEach-Object { | |
gh api -X DELETE "repos/$repo/actions/artifacts/$($_.id)" | |
Write-Output "Deleted artifact ID $($_.id)" | |
} | |
} | |
else { | |
Write-Output "No artifacts were deleted." | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment