Our GitHub organization has a growing pile of merged branches, quiet pull requests, and repos that haven't changed in a long time. What's a practical way to handle cleanup across the org, and what would you avoid automating?
How do you clean up stale branches and repos across a GitHub org?
Answers
A scheduled GitHub Action in an admin repo can use the gh CLI to loop through the organization's repos and produce a cleanup report. Start in report-only mode so people can agree on the rules before anything gets changed.
For pull requests, an inactivity rule can add a label and comment first, then close the PR after a warning period if nobody objects. The actions/stale action is another option for that part. Pick time limits that fit the team rather than treating 90 days as a universal cutoff.
Reply to this answer
Turn on automatic head branch deletion after merge where it makes sense. That stops the pile from growing, but the existing branches need a separate pass.
Be careful with squash merges. Git's merged-branch check may miss them because the squash commit doesn't preserve the branch's ancestry. You can check for a merged PR with that head branch, then verify the current branch head still matches the PR's head commit before considering deletion. Someone may have pushed more work after the merge.
Reply to this answer
For inactive repos, I'd generate a list based on the last commit date and ask the responsible teams whether they're still used. No commits for a year is a useful flag, not proof a repo is dead.
Archive confirmed candidates rather than deleting them. Archiving is reversible, and it gives you a way to reduce clutter without throwing the repo away.
_e29!zavdori.5:
That sounds easier to get agreement on than deleting repos just because they're quiet.
Reply to this answer
I'd keep branch cleanup as a human-reviewed report, at least until the rules are well tested. An old branch can contain a fix or unfinished work that exists nowhere else. Age alone isn't enough reason to delete it.
Flag likely owners and associated work items where you can, and give people a deadline to review the list. I'd also leave tags and release branches out of automatic deletion. They may be needed for an old release or a hotfix.
Reply to this answer
Before building the automation, decide what problem the clutter is causing and who gets to make the call. Iterating through repos is the easy bit. Getting teams to agree on what can be closed or removed is usually trickier.
For unmerged PRs, a quick triage can be more useful than a blanket timer. Is the work unfinished, no longer wanted, or blocked? Close the ones that are no longer needed and leave an explicit reason for the rest.
Reply to this answer
Add your answer
Answers are reviewed before publication.
n.628+plovercrayon:
Keep an eye on whether a comment resets the timer. A quick reply can keep an otherwise abandoned PR open without actually moving it forward.