-
-
Save dijitali/2bb7cc070a22943c0f33324ffcfbd3f0 to your computer and use it in GitHub Desktop.
Remove Old GitHub Actions Artifacts from Repository
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
$repoOwner = "OWNER" | |
$repoName = "REPO_NAME" | |
$mainRetentionDate = (Get-Date).AddDays(-30) | |
$branchRetentionDate = (Get-Date).AddDays(-7) | |
$pageSize = 100 | |
$page = 1 | |
# Retrieve first page of artifacts | |
$response = gh api "repos/$repoOwner/$repoName/actions/artifacts?per_page=$pageSize&page=$page" | ConvertFrom-Json | |
$total_pages = [math]::Ceiling(($response.total_count / $pageSize)) | |
$artifacts = $response.artifacts | |
# Paginate remainder | |
for ($page = 2; $page -le $total_pages; $page++) { | |
$response = gh api "repos/$repoOwner/$repoName/actions/artifacts?per_page=$pageSize&page=$page" | ConvertFrom-Json | |
$artifacts += $response.artifacts | |
} | |
Write-Host ('Total artifact count: ' + $artifacts.Length) | |
Write-Host ('Total artifact size (bytes): ' + ($artifacts | Measure-Object -Property size_in_bytes -Sum).Sum) | |
$spaceSaved = 0 | |
foreach ($artifact in $artifacts) { | |
switch ($artifact.workflow_run_head_branch) { | |
'main' { | |
if ( (Get-Date $artifact.created_at) -lt $mainRetentionDate) { | |
Write-Host ('Removing artifact with id: ' + $artifact.id + ` | |
' name: ' + $artifact.name + ` | |
' created at ' + $artifact.created_at + ` | |
' for branch ' + $artifact.workflow_run.head_branch) | |
#Remove | |
gh api "repos/$repoOwner/$repoName/actions/artifacts/$($artifact.id)" -X Delete --silent | Out-Null | |
$spaceSaved += $artifact.size_in_bytes | |
} | |
} | |
Default { | |
if ( (Get-Date $artifact.created_at) -lt $branchRetentionDate) { | |
Write-Host ('Removing artifact with id: ' + $artifact.id + ` | |
' name: ' + $artifact.name + ` | |
' created at ' + $artifact.created_at + ` | |
' for branch ' + $artifact.workflow_run.head_branch) | |
#Remove | |
gh api "repos/$repoOwner/$repoName/actions/artifacts/$($artifact.id)" -X Delete | Out-Null | |
$spaceSaved += $artifact.size_in_bytes | |
} | |
} | |
} | |
} | |
Write-Host 'Total space saved (bytes): ' $spaceSaved |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example output: