Delete Vercel env vars when branch is deleted

I’m using the Vercel integration to automatically create database branches for my Vercel preview environments.

After deleting a Neon database, the DATABASE_URL and PGHOST environment variables remain set for the preview env in Vercel, and over time, my Vercel project’s settings are becoming littered with configuration for deleted branches.

The integration should automatically delete the associated Vercel environment variables when the database branch is deleted.

1 Like

Thank you, @josh3736, for your feedback. I’ll share this with the team.
Can you help and share more about your Neon branch clean-up process? Do you clean up Vercel previews too?

1 Like

Based on advice in this thread, I set up a GitHub action that runs when a git branch is deleted. It finds and deletes the corresponding Neon DB branch.

name: Delete neon branch
on: delete
jobs:
  delete-neon-branch:
    if: github.event.ref_type == 'branch'
    runs-on: ubuntu-latest
    steps:
      - name: Get neon branch ID
        id: get_branch_id
        run: |
          branch_id=$(curl --silent \
            "https://console.neon.tech/api/v2/projects/${{ secrets.NEON_PROJECT_ID }}/branches" \
            --header "Accept: application/json" \
            --header "Content-Type: application/json" \
            --header "Authorization: Bearer ${{ secrets.NEON_API_KEY }}" \
            | jq -r .branches \
            | jq -c '.[] | select(.name == "'${{ github.event.ref }}'") .id' \
            | jq -r \
            ) \
          
           echo "branch_id=${branch_id}" >> $GITHUB_OUTPUT

      - name: Delete neon branch
        uses: neondatabase/delete-branch-action@beta
        with:
          project_id: ${{ secrets.NEON_PROJECT_ID }}
          branch_id: ${{ steps.get_branch_id.outputs.branch_id }}
          api_key: ${{ secrets.NEON_API_KEY }}

We’re not doing anything to clean up Vercel preview deployments since those don’t really matter or get in the way. Obviously, they stop working with their database gone, but that’s fine.

The environment variable settings page is another matter. It is quickly becoming a nightmare full of old and unnecessary config variables for old git branches, and I really don’t want to have to manually delete things.

I suppose I could write additional code in a GitHub Action to clean up Vercel config in addition to deleting Neon branches, but I’d expect the integration to do that for me. Once a Neon branch is deleted and its compute endpoint destroyed, there’s really no point in leaving the old config vars in Vercel, regardless of whether the preview envs are cleaned up or not.

1 Like