Repository Maintenance Cleanup
Intermediate10 min
Clean up stale branches, close abandoned PRs, and maintain a healthy repository using GitHub CLI automation.
Prerequisites
- -GitHub CLI (gh) installed and authenticated
Steps
1
List stale PRs with no recent activity
Find pull requests that have not been updated recently.
$ gh pr list --state open --json number,title,updatedAt --jq '.[] | select(.updatedAt < (now - 2592000 | todate)) | "#\(.number) \(.title) (updated: \(.updatedAt))"'
2592000 seconds is 30 days. Adjust the number for different staleness thresholds.
2
Close stale PRs with a comment
Close abandoned PRs with a polite explanation.
$ gh pr close 42 --comment 'Closing due to inactivity. Feel free to reopen if you want to continue this work.'
3
Delete merged remote branches
Remove remote branches that have already been merged.
$ gh api repos/{owner}/{repo}/branches --paginate --jq '.[].name' | while read branch; do gh api repos/{owner}/{repo}/pulls --jq ".[] | select(.head.ref==\"$branch\" and .merged_at!=null) | .head.ref" 2>/dev/null; done | sort -u
Enable 'Automatically delete head branches' in repository settings for future PRs.
4
View repository statistics
Get an overview of repository activity and health.
$ gh repo view --json name,stargazerCount,forkCount,openIssues,pullRequests --jq '{name, stars: .stargazerCount, forks: .forkCount}'
Full Script
FAQ
Discussion
Loading comments...