This repository includes a GitHub Actions workflow for safely cleaning up stale branches with multiple safety features, including mandatory approval before deletion.
The cleanup workflow includes the following safety mechanisms:
- Protected Branch Exclusion: Automatically skips branches marked as protected in GitHub
- Pattern-Based Exclusion: Configurable patterns to exclude important branches (e.g.,
main,master,develop,release/*,hotfix/*) - Active PR Check: Branches with open pull requests are never deleted
- Deletion Limits: Configurable maximum number of branches to delete per run
- User Confirmation: Requires explicit confirmation by typing "DELETE" before any deletions occur
- Detailed Logging: Comprehensive console output for complete transparency
- Mandatory Approval: Requires approval from a designated approver or repo admin before deleting branches
- Two-Stage Process: Identification and deletion are separated into two jobs, allowing review before deletion
Before using the cleanup workflow, you must configure the GitHub environment for approvals:
- Go to your GitHub repository Settings
- Navigate to Environments
- Create a new environment named
stale-branch-deletion-approval(exact name required) - Under Deployment protection rules, enable Required reviewers
- Add the approver(s):
- If you specified an approver in the workflow input, add that GitHub user
- If no approver is specified in the workflow input, add repository admins
- Important: At least one approval is mandatory before branches can be deleted
- Click Save protection rules
The cleanup workflow is manual only and must be triggered explicitly:
- Go to the Actions tab in the GitHub repository
- Select the Cleanup Stale Branches workflow
- Click Run workflow
- Configure the workflow parameters:
- Confirm deletion: Type
DELETEto confirm (required) - Days stale: Number of days a branch must be inactive (default: 90)
- Max deletions: Maximum branches to delete in this run (default: 10)
- Exclude patterns: Comma-separated patterns to exclude (default:
main,master,develop,release/*,hotfix/*) - Approver: GitHub handle or email of approver (leave empty to use repo admin as default)
- Confirm deletion: Type
- Click Run workflow
- Wait for the identification job to complete - this will analyze and list all stale branches
- Review the identified branches in the workflow logs
- Approval step: If branches are found, the workflow will pause and wait for approval
- The designated approver (or repo admin if none specified) will receive a notification
- The approver must review and approve the deletion before it proceeds
- Only after approval is granted will the deletion job execute
- The deletion job will execute after approval is granted and delete the identified stale branches
Branch Cleanup:
- Confirm deletion:
DELETE - Days stale:
90 - Max deletions:
10 - Exclude patterns:
main,master,develop,release/*,hotfix/* - Approver:
octocat(or leave empty for repo admin)
This will:
- Identify stale branches (those inactive for 90+ days)
- Display the list in the workflow logs
- Wait for approval from the designated approver
- Delete the approved branches after approval is granted
Note: The two-stage process (identify then delete with approval in between) provides an opportunity to review the identified branches before they are deleted. This eliminates the need for a separate dry run mode.
A branch is considered stale and eligible for deletion if:
- Its last commit is older than the specified number of days
- It is NOT a protected branch
- It does NOT match any exclude patterns
- It does NOT have any open pull requests
- The deletion limit has not been reached
- Approval has been granted by the designated approver or repo admin
- Review the workflow logs after the identification job completes to see which branches were identified
- Start with conservative settings (e.g., 90+ days, low max deletions)
- Review the exclude patterns to ensure important branches are protected
- Check the identification summary before granting approval
- Adjust deletion limits based on your team's workflow and comfort level
- Configure the environment with appropriate approvers before running the workflow
- Carefully review the list of identified branches in the logs before granting approval for deletion