npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

gitlab-stale-branch-cleaner

v1.0.5

Published

CLI to delete stale GitLab branches programmatically

Downloads

176

Readme

GitLab Stale Branch Cleaner

npm downloads license

Safely clean up stale, unmerged GitLab branches using a configurable CLI with dry-run support.
Built for engineering teams that want repository hygiene without accidental deletions.


Why this exists

Over time, GitLab repositories accumulate:

  • Old feature branches
  • Abandoned experiments
  • Forgotten WIP branches

These lead to:

  • Slower navigation
  • Higher cognitive load
  • Maintenance and ownership risk

GitLab Stale Branch Cleaner automates this cleanup in a safe, predictable, and CI-friendly way.


Key Features

  • Dry-run mode (preview before deleting)
  • Configurable stale threshold (e.g. 30 / 60 / 90 days)
  • Protected & custom branch exclusions
  • Targets only stale, inactive branches
  • GitLab CI ready (ideal for scheduled jobs)
  • Simple CLI — no dashboards, no lock-in

Installation

The tool is safe by default and runs in dry-run mode unless explicitly disabled.

No global install required. Run directly using npx:

npx gitlab-stale-branch-cleaner

Or install globally:

npm install -g gitlab-stale-branch-cleaner

Prerequisites

You will need:

  • GitLab Project ID

  • GitLab Personal Access Token

    • Required scope: api

Recommended: set token as an environment variable

export GITLAB_TOKEN=your_token_here

Usage

The CLI supports two modes:

Non-interactive mode (recommended for CI / automation)

Provide all required options via flags.

Interactive mode (local usage)

If required options are not provided, the CLI will prompt interactively:

You will be prompted for:

  • GitLab access token
  • Project ID
  • Main branch
  • Stale days threshold
  • Excluded branches
  • Dry-run preference

Dry-run (recommended first)

npx gitlab-stale-branch-cleaner \
  --project-id 123456 \
  --token $GITLAB_TOKEN \
  --stale-days 60 \
  --dry-run

Example output:

[DRY RUN] Found 12 stale branches
[DRY RUN] Would delete: feature/login-refactor
[DRY RUN] Would delete: bugfix/old-validation

Nothing is deleted in dry-run mode.

Actual deletion

npx gitlab-stale-branch-cleaner \
  --project-id 123456 \
  --token $GITLAB_TOKEN \
  --stale-days 60 \
  --dry-run=false

CLI Options

| Option | Description | Required | | -------------- | -------------------------------------------------- | -------- | | --project-id | GitLab project ID | ✅ | | --token | GitLab API token | ✅ | | --stale-days | Inactivity threshold in days (default: 90) | ❌ | | --dry-run | Preview deletions without deleting (default: true) | ❌ | | --exclude | Comma-separated branch names to exclude | ❌ | | --dry-run | Preview deletions without deleting (default: true) | ❌ | | --help | Show help | ❌ | | --version | Show version | ❌ |

Example:

--exclude main,develop,release

GitLab CI Integration (Recommended)

Run branch cleanup automatically on a schedule:

cleanup_stale_branches:
  image: node:18
  stage: cleanup
  script:
    - npx gitlab-stale-branch-cleaner \
        --project-id $CI_PROJECT_ID \
        --token $GITLAB_TOKEN \
        --stale-days 60 \
        --dry-run=false
  rules:
    - if: '$CI_PIPELINE_SOURCE == "schedule"'

Exit Codes

The CLI uses the following exit codes:

| Code | Meaning | |----|--------| | 0 | Execution successful (including no stale branches found) | | 1 | Invalid usage or missing required inputs | | 2 | Runtime failure (GitLab API error, network issue, or partial deletion failure) |

These exit codes make the tool safe to use in CI pipelines.

Best practice

Run initially with --dry-run=true, review output, then enable deletion.


Safety Guarantees

This tool will not:

  • Delete protected branches

  • Delete recently updated branches

  • Delete anything unless --dry-run=false is explicitly set

Designed to be boring, predictable, and safe.


Security Considerations

  • This tool uses a GitLab Personal Access Token only to:

    • Read branch metadata
    • Delete branches explicitly identified as stale
  • The token is:

    • Never logged
    • Never persisted
    • Never transmitted outside GitLab APIs
  • Recommended practices:

    • Use a token with minimum required scope (api)
    • Prefer environment variables over CLI arguments
    • Rotate tokens periodically

When should you use this?

  • Medium to large GitLab repositories

  • Teams with frequent feature branching

  • CI-driven repo maintenance

  • Engineering teams that value automation & hygiene


Contributing

Issues, suggestions, and PRs are welcome.

If something feels unsafe or unclear, please open an issue — safety comes first.

If this tool saved you time, consider ⭐ starring the repository.


License

MIT