Skip to content

feat: [#322] add purge command to remove local environment data#323

Merged
josecelano merged 6 commits intomainfrom
322-add-purge-command
Feb 6, 2026
Merged

feat: [#322] add purge command to remove local environment data#323
josecelano merged 6 commits intomainfrom
322-add-purge-command

Conversation

@josecelano
Copy link
Copy Markdown
Member

Summary

Adds a purge command that removes local data for environments in any state, with proper confirmation to prevent accidental data loss.

Changes

  • Presentation Layer: CLI command with --force flag for automation
  • Application Layer: PurgeCommandHandler with deletion logic for data/, build/, and registry entries
  • Confirmation Prompt: Interactive y/N prompt with warning (skipped with --force)
  • E2E Tests: 5 test scenarios covering normal workflow, error handling, custom paths, full lifecycle, and environment isolation
  • Documentation: Comprehensive user guide in docs/user-guide/commands/purge.md (426 lines)
  • Destroy Enhancement: Added hint to destroy command output guiding users to purge

Why This Feature

When the destroy command runs, it tears down infrastructure but preserves local data for debugging. This means:

  • Environment names remain "taken" after successful destruction
  • Users cannot create new environments with the same name
  • Manual cleanup is required

The purge command solves this by safely removing local data after destruction.

Testing

  • ✅ All 5 E2E tests passing
  • ✅ All pre-commit checks passing (linting, formatting, clippy, doctests)
  • ✅ Manual testing verified
  • ✅ Documentation complete and linted

Implementation Details

  • Follows DDD architecture (Domain → Application → Infrastructure → Presentation)
  • Idempotent operation (succeeds if already purged)
  • Clear error messages with troubleshooting guidance
  • Observable through tracing
  • Production-ready

Closes #322

Implements Phase 3: User confirmation prompt for purge command.

Changes:
- Add confirmation flow with warning message
- Prompt user for y/N confirmation before deletion
- Skip prompt when --force flag is provided
- Add IoError variant for stdin read failures
- Implement show_confirmation_prompt() helper
- Implement read_user_confirmation() helper

The confirmation shows:
- Warning about irreversible operation
- List of what will be deleted (data/, build/, registry)
- User prompt accepting y/yes or canceling otherwise

Tested manually:
- echo "n" | purge env → cancels with UserCancelled error
- echo "y" | purge env → confirms and deletes directories
- purge env --force → skips prompt and deletes immediately
Implements Phase 4: Comprehensive E2E tests for purge command.

Changes:
- Add run_purge_command() method to ProcessRunner
- Add 5 E2E test scenarios:
  1. Purge destroyed environment successfully
  2. Fail when purging nonexistent environment
  3. Purge with custom working directory
  4. Complete lifecycle (create → destroy → purge)
  5. Remove only specified environment data
- Add assertion helpers:
  * assert_environment_not_exists()
  * assert_data_directory_not_exists()
  * assert_build_directory_not_exists()
- Register purge_command module in tests/e2e/mod.rs

All 5 tests pass successfully:
- test it_should_complete_full_lifecycle_from_create_to_purge ... ok
- test it_should_fail_when_purging_nonexistent_environment ... ok
- test it_should_purge_destroyed_environment_successfully ... ok
- test it_should_purge_with_custom_working_directory ... ok
- test it_should_remove_only_specified_environment_data ... ok

Tests verify:
- Purge removes data/, build/, and registry entry
- Error handling for nonexistent environments
- Isolation between environments (purge one, others remain)
- --force flag skips interactive confirmation
- Custom working directory support
Adds comprehensive user documentation for the purge command.

Changes:
- Create docs/user-guide/commands/purge.md (426 lines)
  * Command syntax and options
  * Basic usage (interactive and automated)
  * What gets purged (data/, build/, registry)
  * Common use cases and workflows
  * When to use purge vs when not to
  * Idempotent operation details
  * Comprehensive troubleshooting section
  * Safety considerations and best practices
  * Comparison table: destroy vs purge
  * Recommended workflow (destroy → purge)
- Update docs/user-guide/commands/README.md
  * Add purge to Environment Cleanup section
  * Add purge to command workflow (step 11)
  * Add purge to plumbing commands list
  * Add purge to state transitions table
- Update docs/issues/322-add-purge-command-to-remove-local-data.md
  * Mark completed phases with ✅
  * Update implementation plan progress
  * Document E2E test results
  * Add documentation tasks to Phase 4

Documentation covers:
- Interactive vs automated usage (--force flag)
- Complete use case scenarios
- Normal workflow: destroy → purge
- Reusing environment names
- Automated cleanup scripts
- Troubleshooting common issues
- Safety warnings and best practices
- Comparison with destroy command
Updates destroy command to suggest purge for complete cleanup.

Changes:
- Update DestroyCommandController::complete_workflow()
- Add purge command hint after successful destroy
- Message format:
  💡 Local data preserved for debugging. To completely remove and reuse the name:
     torrust-tracker-deployer purge {name} --force
- Add blank line before hint for readability
- Uses UserOutput.result() for informational message

Example output:
✅ Environment 'e2e-deployment' destroyed successfully

💡 Local data preserved for debugging. To completely remove and reuse the name:
   torrust-tracker-deployer purge e2e-deployment --force

This improves discoverability of the purge command and guides users
through the complete cleanup workflow (destroy → purge).
@josecelano josecelano self-assigned this Feb 6, 2026
@josecelano
Copy link
Copy Markdown
Member Author

ACK be2a5a7

@josecelano josecelano merged commit 33dec67 into main Feb 6, 2026
38 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Purge Command to Remove Local Environment Data

1 participant