delete
The delete command removes a branch and its associated database snapshot.
pgbranch delete <branch> [flags]Alias: rm
pgbranch rm <branch>Arguments
Section titled “Arguments”| Argument | Description |
|---|---|
branch | Name of the branch to delete (required) |
| Flag | Short | Description | Default |
|---|---|---|---|
--force | -f | Force delete even if it’s the current branch | false |
Examples
Section titled “Examples”Delete a Branch
Section titled “Delete a Branch”pgbranch delete feature-oldOutput:
Branch 'feature-old' deletedUsing Alias
Section titled “Using Alias”pgbranch rm feature-oldForce Delete Current Branch
Section titled “Force Delete Current Branch”pgbranch delete feature-current --forceForce deleting the current branch will leave you with no active branch. You’ll need to checkout another branch or create a new one.
What Gets Deleted
Section titled “What Gets Deleted”When you delete a branch:
- Template database - The PostgreSQL template database (
pgbranch_<name>_<hash>) is dropped - 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.jsonError Cases
Section titled “Error Cases”Branch Doesn’t Exist
Section titled “Branch Doesn’t Exist”pgbranch delete nonexistentError: branch 'nonexistent' does not existDeleting Current Branch Without Force
Section titled “Deleting Current Branch Without Force”pgbranch delete current-branchError: cannot delete current branch 'current-branch' without --forceLast Remaining Branch
Section titled “Last Remaining Branch”Deleting all branches is allowed but will leave you without any snapshots:
pgbranch delete main --forceAfter this, you’ll need to create a new branch to continue using pgbranch.
Bulk Deletion
Section titled “Bulk Deletion”To delete multiple branches, use a loop:
# Delete all feature branchesfor branch in $(pgbranch branch | grep feature); do pgbranch delete "$branch"doneOr use the prune command for automatic cleanup:
# Delete branches not accessed in 7 dayspgbranch prune -d 7 -yRecovering Deleted Branches
Section titled “Recovering Deleted Branches”If you have remotes configured, you may be able to recover from remote storage:
pgbranch pull feature-old --remote originBest Practices
Section titled “Best Practices”Before Deleting
Section titled “Before Deleting”-
Verify you don’t need the branch
Terminal window pgbranch log # Review all branches -
Push to remote if needed
Terminal window pgbranch push feature-old --remote origin -
Check disk usage
Terminal window psql -c "SELECT pg_size_pretty(pg_database_size('pgbranch_feature_old_abc123'))"
Regular Cleanup
Section titled “Regular Cleanup”Use prune for automated cleanup:
# Interactive pruning of stale branchespgbranch prune -d 14
# Automatic pruning (skip confirmation)pgbranch prune -d 14 -yRemote Branch Deletion
Section titled “Remote Branch Deletion”To delete a branch from remote storage:
pgbranch remote delete feature-old --remote originThis only removes the remote copy; the local branch remains.
Related Commands
Section titled “Related Commands”branch- List or create branchesprune- Bulk delete stale branchesremote delete- Delete from remote storage