Prune
The prune command removes branches that haven’t been accessed recently, helping you manage disk space.
pgbranch prune [flags]| Flag | Short | Description | Default |
|---|---|---|---|
--days | -d | Days after which a branch is stale | 7 |
--force | -y | Skip confirmation, prune all stale | false |
Examples
Section titled “Examples”Interactive Pruning
Section titled “Interactive Pruning”pgbranch pruneOutput:
Stale branches (not accessed in 7 days):
1. feature-old-api Last accessed: 2024-01-05 14:30:00 Size: 45 MB
2. experiment-caching Last accessed: 2024-01-03 09:15:00 Size: 52 MB
3. bugfix-login Last accessed: 2024-01-02 16:45:00 Size: 43 MB
Total: 3 branches, 140 MB
Enter branch numbers to KEEP (comma-separated), or press Enter to prune all:- Enter
1,3to keep branches 1 and 3, prune branch 2 - Press Enter to prune all listed branches
Custom Stale Threshold
Section titled “Custom Stale Threshold”pgbranch prune -d 14This considers branches stale if not accessed in 14 days.
Automatic Pruning
Section titled “Automatic Pruning”pgbranch prune -d 7 -yPrunes all stale branches without confirmation. Useful for automation.
Preview Only
Section titled “Preview Only”To see what would be pruned without actually pruning:
pgbranch prune -d 7# Then enter all branch numbers to keep themHow Staleness is Determined
Section titled “How Staleness is Determined”A branch is considered stale if it hasn’t been:
- Checked out
- Created
- Updated (via merge or push/pull)
in the specified number of days.
The “last accessed” time is stored in branch metadata.
What Gets Deleted
Section titled “What Gets Deleted”When a branch is pruned:
- Template database is dropped
- Branch metadata is removed
PRUNE branch 'feature-old' │ ├──▶ DROP DATABASE pgbranch_feature_old_abc123 │ └──▶ rm .pgbranch/branches/feature-old.jsonProtected Branches
Section titled “Protected Branches”The current branch is never pruned, even if stale:
pgbranch pruneStale branches (not accessed in 7 days):
Note: Current branch 'main' is excluded from pruning.
1. feature-old ...Disk Space Recovery
Section titled “Disk Space Recovery”After pruning, check recovered space:
# Before pruningpsql -c "SELECT pg_size_pretty(sum(pg_database_size(datname))) FROM pg_database WHERE datname LIKE 'pgbranch_%'"
# Prunepgbranch prune -d 7 -y
# After pruningpsql -c "SELECT pg_size_pretty(sum(pg_database_size(datname))) FROM pg_database WHERE datname LIKE 'pgbranch_%'"Automated Cleanup
Section titled “Automated Cleanup”Cron Job
Section titled “Cron Job”Set up automatic weekly cleanup:
# Edit crontabcrontab -e
# Add line (runs every Sunday at 3 AM)0 3 * * 0 cd /path/to/project && pgbranch prune -d 14 -y >> /var/log/pgbranch-prune.log 2>&1Pre-commit Hook
Section titled “Pre-commit Hook”Clean up before commits:
#!/bin/shpgbranch prune -d 7 -yCI/CD Pipeline
Section titled “CI/CD Pipeline”Clean up in your pipeline:
# GitHub Actions example- name: Cleanup old branches run: | pgbranch prune -d 3 -yBest Practices
Section titled “Best Practices”Regular Pruning
Section titled “Regular Pruning”Run prune regularly to keep disk usage manageable:
# Weekly manual checkpgbranch prune -d 14
# Or automatepgbranch prune -d 7 -yBefore Major Operations
Section titled “Before Major Operations”Prune before operations that need disk space:
# Before pulling large branchespgbranch prune -d 7 -ypgbranch pull large-datasetProject-Specific Thresholds
Section titled “Project-Specific Thresholds”Different projects may need different thresholds:
| Project Type | Suggested -d |
|---|---|
| Active development | 7 days |
| Maintenance mode | 30 days |
| Archived/reference | 90 days |
Recovering Pruned Branches
Section titled “Recovering Pruned Branches”If you have remotes configured, branches may be recoverable:
# Check if branch exists on remotepgbranch remote ls-remote
# Pull if availablepgbranch pull feature-oldBackup Before Pruning
Section titled “Backup Before Pruning”For important branches, push to remote first:
# Push all branches to remotefor branch in $(pgbranch branch); do pgbranch push "$branch" --forcedone
# Then prune locallypgbranch prune -d 7 -yTroubleshooting
Section titled “Troubleshooting”Cannot Prune Current Branch
Section titled “Cannot Prune Current Branch”Error: cannot prune current branchSwitch to a different branch first:
pgbranch checkout mainpgbranch pruneDatabase Connection Error
Section titled “Database Connection Error”Error: could not connect to databaseEnsure PostgreSQL is running and accessible.
Permission Denied
Section titled “Permission Denied”Error: permission denied to drop databaseYour PostgreSQL user needs DROP DATABASE permission.