Skip to content

delete

The delete command removes a branch and its associated database snapshot.

Terminal window
pgbranch delete <branch> [flags]

Alias: rm

Terminal window
pgbranch rm <branch>
ArgumentDescription
branchName of the branch to delete (required)
FlagShortDescriptionDefault
--force-fForce delete even if it’s the current branchfalse
Terminal window
pgbranch delete feature-old

Output:

Branch 'feature-old' deleted
Terminal window
pgbranch rm feature-old
Terminal window
pgbranch delete feature-current --force

Force deleting the current branch will leave you with no active branch. You’ll need to checkout another branch or create a new one.

When you delete a branch:

  1. Template database - The PostgreSQL template database (pgbranch_<name>_<hash>) is dropped
  2. Branch metadata - The branch configuration file in .pgbranch/branches/ is removed
DELETE branch 'feature-old'
├──▶ DROP DATABASE pgbranch_feature_old_abc123
└──▶ rm .pgbranch/branches/feature-old.json
Terminal window
pgbranch delete nonexistent
Error: branch 'nonexistent' does not exist
Terminal window
pgbranch delete current-branch
Error: cannot delete current branch 'current-branch' without --force

Deleting all branches is allowed but will leave you without any snapshots:

Terminal window
pgbranch delete main --force

After this, you’ll need to create a new branch to continue using pgbranch.

To delete multiple branches, use a loop:

Terminal window
# Delete all feature branches
for branch in $(pgbranch branch | grep feature); do
pgbranch delete "$branch"
done

Or use the prune command for automatic cleanup:

Terminal window
# Delete branches not accessed in 7 days
pgbranch prune -d 7 -y

If you have remotes configured, you may be able to recover from remote storage:

Terminal window
pgbranch pull feature-old --remote origin
  1. Verify you don’t need the branch

    Terminal window
    pgbranch log # Review all branches
  2. Push to remote if needed

    Terminal window
    pgbranch push feature-old --remote origin
  3. Check disk usage

    Terminal window
    psql -c "SELECT pg_size_pretty(pg_database_size('pgbranch_feature_old_abc123'))"

Use prune for automated cleanup:

Terminal window
# Interactive pruning of stale branches
pgbranch prune -d 14
# Automatic pruning (skip confirmation)
pgbranch prune -d 14 -y

To delete a branch from remote storage:

Terminal window
pgbranch remote delete feature-old --remote origin

This only removes the remote copy; the local branch remains.