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.
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.
What does this prompt do?
- 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 do I need to use this?
- 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-
How can I customize it?
- 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.
FAQs
What happens if it removes a branch I still needed?
Could this ever delete my production database?
What if a branch does not match any pull request?
Neon can expire branches automatically now. Why do I need this?
Will I actually save much?
Related templates
When your flight moves, your calendar times get corrected automatically and you get a Slack note naming the meetings you're about to miss.
Every 15 minutes, forwarded phishing reports get traced back to the server that really sent them, with a verdict in Slack and the worst senders reported.
Every weekday at 7am, sign in to the tender portals you track, filter new notices against your bid criteria, and open a deal for the ones worth chasing.
When you merge a fix in GitHub, this agent checks the matching dead-letter queue, replays the failed messages, and reports back on the pull request and in Slack.
Every Monday, rank the week's matches by expected demand, put the big ones on your venue calendar, and post a rota-ready summary to Slack.
Every Tuesday we pull your best new reviews, draft social captions, email testimonials and product page quotes, then stage them in Notion for approval.
Stop paying for preview databases nobody uses.
Set this up once and your Neon bill stops quietly growing with every sprint.