Open History to see the last commit
1import { createNeonClient } from '@neon/sdk'
2
3// Trusted CI tooling only; never imported by the library or Function runtime.
4const { NEON_API_KEY: apiKey, NEON_PROJECT_ID: projectId } = process.env
5if (!apiKey || !projectId) throw new Error('Project-scoped Neon credentials required')
6const neon = createNeonClient({ apiKey, throwOnError: true, waitForReadiness: true })
7const cutoff = Date.now() - 26 * 3600_000
8const branches = await neon.branches.list({ projectId }).all()
9// These names are reserved for this repository's disposable test tooling.
10const owned = /^ds-test-(?:\d+(?:-\d+)?(?:-(?:hints|load))?|(?:recovery|inplace|preserved)-[a-f0-9-]{36})$/
11const stale = branches.filter((branch) => owned.test(branch.name) && !branch.default && !branch.protected
12 && Date.parse(branch.created_at) < cutoff).sort((a, b) => Date.parse(b.created_at) - Date.parse(a.created_at))
13let removed = 0
14const remaining = new Set(branches.map((branch) => branch.id))
15for (const branch of stale) {
16 // Refuse any parent with a live/unrecognized child, even if its name matches.
17 if (branches.some((child) => child.parent_id === branch.id && remaining.has(child.id))) continue
18 await neon.branches.delete({ projectId, branchId: branch.id })
19 remaining.delete(branch.id)
20 removed++
21}
22console.log(`Deleted ${removed} stale disposable test branches.`)