Daily
Delete old preview branches nightly
Preview branches past a set age are removed each night, and you get the list of what went.
- Runs
- Daily
- Apps
- Neon
- AI
- Not needed
- Access
- read+write
Send this to your assistant
Create a ClickRuntime automation: every night, delete Neon branches older than 14 days except main and anything starting with release-, and email me the names removed. Show me a dry run first.
First time? Adding ClickRuntime to your assistant takes about two minutes.
Why use it
Why teams use this automation
Neon makes branching so cheap that preview branches pile up: one per pull request, one per demo, one for the contractor who left in the spring. Each holds a copy of data, counts toward your project's branch limit and storage, and some contain customer records copied from production. Nobody is responsible for deleting them, and a developer eventually hits the branch limit in the middle of a release.
Every day ClickRuntime lists the branches in your Neon project, filters for those that match a naming pattern such as preview/ and are older than 14 days, deletes them, and emails the list of what was removed. The main branch and anything that does not match the pattern are left alone.
Engineering leads who use Neon with per-pull-request environments use it as routine housekeeping.
What you get out of it
Branch limits stop interrupting releases
Old previews are cleared before they reach the project cap. A developer can create the branch they need on release day without first hunting for something safe to delete. Release day does not start with a search for safe branches to delete.
Storage and compute costs stay predictable
Every leftover branch keeps data that you pay to hold. Removing two-week-old previews each day stops forgotten copies from adding to the Neon bill month after month. Leftover branches keep data that you pay to store, month after month.
Stale copies of data are removed
Preview branches often contain production-like records. Deleting them on a fixed schedule reduces how long personal data sits in places nobody remembers, which helps with data protection duties. That also shortens the list of places a data request would have to cover.
Nobody has to remember cleanup
The task belongs to a schedule instead of the person who last complained. Teams stop relying on a quarterly clean-up day that only happens if someone is annoyed enough. Cleanup is owned by a schedule, not by whoever is annoyed enough.
A written record of every deletion
The emailed list names each branch removed and when it was created. If a developer says their branch vanished, you can show it was past the age limit. Showing that a branch was past the limit settles any argument quickly.
How it works
What happens, step by step
Every night ClickRuntime lists the branches in your Neon project.
It picks out preview branches older than the age you set, matching the names you chose.
It deletes them, or just lists what it would delete while you are trying it out.
You get an email listing the branches removed.
Before it runs live
- Connect Neon
What you get
Build your own
Need it to work differently?
Your own rules
Conditions, thresholds, filters and exceptions, written the way you'd explain them to a colleague.
Any number of apps
One automation can read from one app and update several others. Over 248 apps are ready, and anything with an API works too.
AI where it helps
Summaries, classification, extraction and drafts use ClickRuntime's built-in AI, with no extra key.
On a schedule or on each event
Runs as often as every 5 minutes, or the moment an app sends an event, and keeps track of what it already handled.
Changes you can ask for
- Run it at a different time, more or less often, or only on weekdays
- Add an AI step, for example to summarize, sort or draft a reply
- Add another app, for example also send it to Slack
- Combine it with other Neon steps into one automation
Under the hood
The code that runs this automation
from datetime import datetime, timedelta, timezone
from clickruntime import on_trigger, credentials, http, notify
NEON = "https://console.neon.tech/api/v2"
def neon(method, path, **kwargs):
resp = http.request(method, f"{NEON}{path}", headers={"Authorization": f"Bearer {credentials.secret('NEON_API_KEY')}"}, **kwargs)
resp.raise_for_status()
return resp.json()
PROJECT_ID = "your-neon-project-id"
DRY_RUN = True # set to False once the list looks right
@on_trigger
def run(event):
cutoff = datetime.now(timezone.utc) - timedelta(days=14)
removed = []
for b in neon("GET", f"/projects/{PROJECT_ID}/branches")["branches"]:
old = datetime.fromisoformat(b["created_at"].replace("Z", "+00:00")) < cutoff
if old and not b.get("default") and b["name"] != "main" and not b["name"].startswith("release-"):
if not DRY_RUN:
neon("DELETE", f"/projects/{PROJECT_ID}/branches/{b['id']}")
removed.append(b["name"])
title = "Would delete" if DRY_RUN else "Deleted"
notify.email_me(f"{title} {len(removed)} Neon branches", "\n".join(removed) or "Nothing to remove.")
return {"dry_run": DRY_RUN, "branches": removed}FAQ
Common questions
Can it protect specific branches?
Is deletion reversible?
How is a branch's age calculated?
What permissions does it need?
Does it need AI?
How many runs does it use?
Can I change how it works?
Related
You might also like
Get a weekly Neon usage report
Each Monday you see compute hours and storage for every Neon project in one email.
See how it worksGet a warning before storage runs out
You get one alert when a project's storage passes the size you set, not a repeat every check.
See how it worksCreate a dated backup branch every night
Your main branch is copied to a dated branch each night, and only the last seven are kept.
See how it worksKeep exploring
Set up “Delete old preview branches nightly” from your assistant
Copy the prompt above, or connect your assistant first. Free plan, no credit card.