Vercel integration, branches are not deleted after merge and branch deletion on github?

Dear team, I was wondering why branches are not deleted after the branch on github is deleted with the Vercel integration. Is there any particular reason?
Doesn’t this creates a lot of unwanted residue branches over time?

Thank you!

1 Like

Hi @ThibaultJanBeyer ,

One of the reasons we do not support deleting branches is that they are tied to your Vercel previews.
Do you delete your Vercel previews? What is your process for cleaning up your Vercel previews?

One possible way is to delete Neon branches using Delete Branch GitHub Action on merge.

You can find the full examples here: deploy to preview and deploy to production.

name: Deploy Production

on:
  push:
    branches:
      - 'main'

jobs:
  deploy-production:
    permissions: write-all
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Search branch by name
        id: get_branch_id
        # list all branches and filter by name
        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 | contains("'$BRANCH_NAME'")) .id' \
            | jq -r \
            ) \
            
          echo "branch_id=${branch_id}" >> $GITHUB_OUTPUT

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

You can find the full code example here.

Let me know if this helps.

2 Likes

This makes sense, thank you!

Unfortunately this script does fail for me with:

Error: Unable to process file command 'output' successfully.
Error: Invalid format 'br-royal-wood-490981'

@Raouf_Chebri

Ok so finally figured out, the issue is that $BRANCH_NAME is not an environment variable. I ended up using GitHub - tj-actions/branch-names: Github action to retrieve branch or tag names with support for all events. to get the current branch name and it works :+1:

1 Like

Hello,
Just a couple points to mention for any future people arriving here. This has been a problem for me for the last couple months so glad it was solved, and thank you @ThibaultJanBeyer for making the post.

  1. The block of code above is not the same as the full code example link so beware of that. I used what was mentioned above.

  2. An easy way to get the branch name is simply do ${{ github.event.ref }}
    github.event.* context is the event payload so for delete you have a ref property available to you, no need for the extra action to fetch the branch.

I personally have not been deleting my Vercel preview branches. I didn’t think it was necessary however now i’m debating on doing a similar action but to delete Vercel preview branches on branch delete

1 Like

Hey @Shaun,

Thanks for your feedback.

  1. The block of code above is not the same as the full code example link so beware of that. I used what was mentioned above.

I edited the post to include the links to the repo before the code block.

Thank you.