Clean up stale PlanetScale branches before they cost you money

By General Input

Every morning, find the development branches nobody is using anymore, skip anything with a migration still in flight, delete the rest, and post a Slack recap.

Integrations

  • PlanetScale
  • Slack Bot

Type

Deterministic Code

Categories

  • Engineering
  • Operations

Every day at 7am, clean up development branch sprawl across our PlanetScale databases before it quietly costs us money. Build this as a deterministic code workflow on a cron trigger. Every step is a known operation, so no agent reasoning is needed.

Expose these as configurable inputs with sensible defaults: the PlanetScale organization name, the age threshold in days (default 14), a list of protected branch names that must never be deleted (default should include staging), and the Slack channel that receives the summary.

Step one, use PlanetScale List databases to get every database in the configured organization. For each database, use PlanetScale List branches to get its branches. Both of these endpoints are paginated, so page through until there are no more results rather than reading only the first page.

Step two, select the deletion candidates. From each database's branches, drop the production branch, identified by the branch's production flag rather than by guessing at its name. Drop any branch whose name appears in the configured protected list. From what remains, keep only branches whose created_at timestamp is older than the age threshold, comparing against the current time in UTC.

Step three, protect in flight migrations. For each database, use PlanetScale List deploy requests and collect the branch names attached to any deploy request that is still open. Remove those branches from the candidate list. This rule matters more than the cleanup itself, so an unfinished migration is never destroyed, and if the deploy request lookup fails for a database, skip that database's deletions entirely rather than deleting without the safety check.

Step four, use PlanetScale Delete a branch on each remaining candidate. If an individual delete fails, do not abort the whole run. Record the failure and carry on to the next branch, then include the failures in the summary.

Step five, post exactly one Slack message to the configured channel using the Slack Bot Send a Message operation. Group the results by database, listing the branch names that were deleted along with their age in days, and the count of branches remaining on that database afterwards. Add a short trailing line covering what was intentionally skipped and why, separating branches held back for an open deploy request from ones excluded as protected or production. Post the message even when nothing was deleted, stating that the check ran and found nothing old enough, so that a silent failure never looks the same as a clean result. Format the message with Slack mrkdwn, using single asterisks for bold.

Related prompts

Explore more prompts
A brand asset library your marketing team actually searchesTurn Mailjet email clicks into ranked HubSpot follow-upsClean out the Looker dashboards and Looks nobody opensLiveKit live operations console for room moderationWake up dormant Keap leads with a researched reasonLiveChat coverage board for planning next week's shiftsPhone routing control panel for LiveKit voice agentsLinkedIn Ads budget pacing dashboard for every client accountGive your team Looker numbers without buying more seatsPause marketing emails to escalated customers, then restore them