# Weekly cleanup of stale Neon branches that cost you money

> Every Monday, find the leftover preview databases nobody is using, check them against closed pull requests, remove the safe ones, and report back in Slack.

- Workflow type: agent
- Services: Neon, GitHub, Slack
- Categories: Engineering, Operations
- Published: 2026-08-07

## What it does

- Checks every one of your Neon projects each Monday morning and surfaces the branches nobody has touched in weeks
- Matches each leftover branch to the pull request it came from, so you can see which ones belong to work that already shipped or got abandoned
- Removes branches whose pull request is closed and that have sat untouched for more than two weeks, while leaving your default and protected branches completely alone
- Posts a Slack summary covering what was removed, what was kept and why, and how much compute each removed branch had been quietly burning

## What you'll need

- A Neon account with access to the projects you want swept
- A GitHub login for the repositories whose pull requests should be checked
- A Slack workspace and a channel where the weekly summary should land
- A short list of branch names you never want touched, such as main, prod, or anything starting with keep-

## Prompt

Every Monday at 8am, sweep my Neon projects for stale preview branches that are quietly running up storage and compute costs, and clean up the ones that are safe to remove.

Start with Neon List projects to get every project I have access to. For each project, use List branches sorted by updated_at so the least recently touched branches surface first, and use Retrieve branch consumption metrics to find out how much compute and storage each branch has been consuming. Use Retrieve branch details when you need a branch's parent, size, or exact timestamps to make a judgement call.

Then cross-reference the branch names against GitHub using List Pull Requests, filtering by state so you can tell which pull requests are already closed or merged and which are still open. Preview branches are normally named after the pull request or the git branch they were created for, so match on that naming convention, and do not force a match that is not really there.

Delete a branch with Delete branch only when both conditions hold: its pull request is closed or merged, and the branch has not been updated in more than 14 days. Never touch a project's default branch, and never touch any branch matching a protected pattern such as main, prod, or keep-*.

When a branch is ambiguous, list it in the Slack summary as a suggestion rather than deleting it. Ambiguous means there is no matching pull request at all, or there is an open pull request that looks abandoned. I would rather review those myself than have you guess.

Finish by posting a summary to Slack with Send a Message. Cover what was deleted, what was kept and why, and how much compute each deleted branch had been burning so I can see where the savings came from. Group the results by project, and put the ambiguous suggestions in their own clearly labelled section.

Close the summary with a reminder that Neon soft-deletes branches for seven days, so anything removed by mistake can still be restored with Recover a deleted branch inside that window.

## How to customize

- Change the schedule. Monday at 8am works well as a start-of-sprint tidy-up, but Friday afternoon or the first of the month works just as well.
- Adjust how old a branch has to be before it counts as stale. Two weeks is a safe default, but a fast-moving team might drop it to one.
- Add your own protected name patterns so branches like demo, release, or anything belonging to a long-running project never get removed.

## FAQ

### What happens if it removes a branch I still needed?

Neon keeps deleted branches for seven days before they are gone for good, so a mistake is recoverable. The weekly summary reminds you of this every time it runs, along with the names of everything it removed.

### Could this ever delete my production database?

No. The workflow always skips each project's default branch, and it skips anything matching a protected name pattern such as main, prod, or keep-anything. You can add more patterns of your own.

### What if a branch does not match any pull request?

It gets listed in the Slack summary as a suggestion for you to look at, never deleted. The same goes for a branch whose pull request is still technically open but looks abandoned. Anything ambiguous is your call, not the workflow's.

### Neon can expire branches automatically now. Why do I need this?

Built-in expiration only applies to branches created after you turn it on, so it does nothing about the sprawl you have already accumulated. A recurring sweep clears the existing backlog and keeps catching branches that were created without an expiry set.

### Will I actually save much?

It depends on how many preview branches your team creates, but storage is the cost that compounds quietly month over month. Once a branch falls outside your history retention window it starts adding to your bill, and most teams never notice until it is already expensive.

Use this prompt in General Input: https://www.generalinput.com/prompts/weekly-cleanup-of-stale-neon-branches-that-cost-you-money