See llms.txt for all machine-readable content.

Back to Templates

Clean up stale GitHub repos, branches and pull requests with GitHub API

Quick overview

This workflow runs on a schedule to maintain a GitHub organization by labeling inactive pull requests as stale and closing them later, deleting old non-default branches, and archiving repositories that have been inactive for 90+ days.

How it works

  1. Runs on a scheduled trigger.
  2. Fetches all repositories in a specified GitHub organization and continues only for repositories that are not already archived.
  3. For each repository, retrieves open pull requests and labels PRs that have not been updated for more than 5 days with a "stale" label, then posts a comment warning they will be closed in 5 days.
  4. For PRs already labeled "stale", fetches the PR issue events, finds when the stale label was applied, and closes the PR if the label is older than 5 days.
  5. Retrieves the repository’s branches, filters out the default branch, and deletes branches whose latest commit is older than 30 days.
  6. Checks how long the repository has been inactive based on its last update time and archives the repository if it has not been updated for more than 90 days.

Setup

  1. Create or select a GitHub App credential in n8n with permissions to read repositories/PRs and to comment, label, close PRs, delete refs (branches), and archive repositories.
  2. Set the GitHub organization name in the organization lookup so it matches the org you want to maintain.
  3. Configure the schedule interval to control how often cleanup runs.
  4. Ensure the target repositories allow branch deletion and that your GitHub App has access to all repositories you want this workflow to manage.