clean-backups command helps you manage disk space by deleting old backup directories while retaining the most recent backups for recovery purposes.
Usage
What It Does
The clean-backups command:- Scans the
.env-twin/directory for backup directories - Sorts backups by timestamp (newest first)
- Identifies old backups beyond the retention count
- Displays which backups will be deleted
- Prompts for confirmation (unless
--yesis used) - Deletes old backup directories and their contents
- Reports the cleanup summary
Deleted backups cannot be recovered. Always review the list of backups to be deleted before confirming.
Options
Number of recent backups to keepBackups are sorted by timestamp, and the N most recent backups are retained. Older backups are deleted.Example:This will keep only the 5 most recent backups and delete all older ones.
Skip confirmation prompt and proceed with cleanup immediatelyAliases:
-yExample:Display help information for the clean-backups commandAliases:
-hExample:Examples
Default cleanup (keep 10 most recent)
Default cleanup (keep 10 most recent)
Clean up old backups, keeping the default 10 most recent:Output:
Keep only 5 most recent backups
Keep only 5 most recent backups
Reduce backup retention to 5 backups:Output:
Auto-confirm cleanup with --yes
Auto-confirm cleanup with --yes
Skip the confirmation prompt for automated scripts:Output:This is useful for:
- Automated maintenance scripts
- Scheduled cleanup tasks
- CI/CD pipeline cleanup stages
No backups to delete
No backups to delete
When you have fewer backups than the keep count:Output:The command exits without prompting since no cleanup is needed.
No backups exist
No backups exist
When no backups are found:Output:
Backup Sorting
Backups are sorted by timestamp in descending order (newest first):--keep option determines how many of the newest backups to retain.
Use Cases
Regular maintenance
Regular maintenance
Run cleanup regularly to prevent disk space issues:Add this to your scheduled tasks or cron jobs:
Before deployment
Before deployment
Clean up old backups before deploying to reduce repository size:
Disk space management
Disk space management
When running low on disk space:
Archive old projects
Archive old projects
Before archiving a project, keep minimal backups:
Backup Directory Structure
The.env-twin/ directory contains timestamped backup folders:
Best Practices
Keep enough backups for recovery
Keep enough backups for recovery
Don’t keep too few backups. A good rule of thumb:
- Development: Keep 5-10 backups
- Staging: Keep 10-20 backups
- Production: Keep 20-30 backups
Review before deleting
Review before deleting
Always review the list of backups to be deleted:Avoid using
--yes until you’re confident in your keep count.Automate with caution
Automate with caution
When automating cleanup, use a conservative keep count:
Check backup age before cleanup
Check backup age before cleanup
Comparison with Manual Deletion
Manual Deletion
Pros:
- Full control over what’s deleted
- Can inspect each backup individually
- Time-consuming
- Error-prone
- Must manually sort by date
- Risk of deleting wrong backups
clean-backups Command
Pros:
- Automatic sorting by timestamp
- Safe retention of recent backups
- Confirmation prompts
- Batch operation
- Scriptable and automatable
- Less granular control
Related Commands
sync
Creates the backups that clean-backups manages
restore
Restore from backups before cleaning them
Troubleshooting
No backups found
No backups found
Problem: Command reports “No backups found in .env-twin/ directory”Solution: Create backups first by running sync:
Permission denied when deleting
Permission denied when deleting
Problem: Cannot delete backup directoriesSolution: Check directory permissions:
Backup directory is not empty after cleanup
Backup directory is not empty after cleanup
Problem: .env-twin/ still contains files after cleanupSolution: Some backups were kept (this is expected). To see what’s kept:If you want to delete all backups:
Accidentally deleted needed backups
Accidentally deleted needed backups
Problem: Deleted backups you neededSolution: Unfortunately, deleted backups cannot be recovered. To prevent this:
- Always review the deletion list before confirming
- Use a conservative
--keepvalue - Avoid using
--yesunless you’re certain
- Version control for
.env-twin/directory (with.gitignorefor sensitive files) - External backup solutions
Safety Features
The clean-backups command includes several safety features:- Confirmation Prompt: Always asks for confirmation unless
--yesis used - Detailed Preview: Shows exactly which backups will be deleted
- File List Display: Shows which files are in each backup
- Keep Count Validation: Ensures at least some backups are retained
- Helpful Hints: Provides usage examples and alternative commands
The command will never delete backups if your total backup count is less than or equal to the keep count.