Recently, I had the feeling that my GitHub repos were starting to get a bit too much. I haven't touched many of them for years, let alone actually used them properly. So I wanted to clean up all my repos and archive everything older than x months/years. But I didn't feel like going through 500 repos manually.
This could be done quite easily with the GitHub CLI. To do this, the GitHub CLI must be installed:

The script
As a Windows user, it made sense for me to just set up the script with PowerShell. In principle, the script only uses the GitHub CLI and retrieves all repos from the account (you have to be logged in, of course) and archives them or, if desired, switches them to private.
$org = "Disane87"
$limit = 400
$retentionDays = 182
(gh repo list $org -L $limit --json name,pushedAt) | ConvertFrom-Json | ForEach-Object {
$repoData = $_;
Write-Host "--------------------------------------------------"
Write-Host "RepoName $($repoData.name)"
$publishedDate = [datetime]::ParseExact($repoData.pushedAt, "M/d/yyyy H:mm:ss", $null)
$timeDiff = New-TimeSpan -Start $publishedDate -End (Get-Date)
if ($timeDiff.TotalDays -gt $retentionDays) {
Write-Host "$($repoData.name) is older than $($retentionDays) days $($timeDiff.TotalDays)"
gh repo edit "$($org)/$($repoData.name)" --visibility private
gh repo archive $repoData.name -y
}
}In principle, you only need to enter your GitHub user name in the script and change the retention if necessary. By default, this is 182 days.
If you do not want to set your repos to private, then simply comment this line out with #
gh repo edit "$($org)/$($repoData.name)" --visibility privateEt voila, your GitHub is cleaned up:

Why archive repos?
Over the years, most developers accumulate hundreds of repositories – test projects, tutorials, forks they never needed. This not only makes overview more difficult but can also lead to security issues: Outdated repos with known vulnerabilities in their dependencies still get flagged by Dependabot, leading to a flood of notifications.
Archived repos are still publicly visible and can be cloned, but no new issues, pull requests, or commits can be created. This also clearly signals to other developers that the project is no longer actively maintained.
Extending the automation
The PowerShell script can be easily extended. For example, you could add a whitelist that excludes certain repos from archiving, or send a notification via email or Discord when repos are archived. A combination with GitHub Actions is also possible to regularly automate the cleanup process.
Further reading
If you enjoyed this article, you might also find these interesting:
Discover more articles
Hide your own page views in Umami
Do you also use Umami to equip your websites with statistics and analyze them and it annoys you that your own visits appear there? We can solve it!
Eigene API mit NodeJS und Express
Du willst dir eine eigene API für deine Dienste bauen? Wir machen es zusammen 👇
Spoolman: Your filament reel management
Lost track of your filaments? I have a solution for that! 🛵