The purge command permanently removes all local data for a deployment environment. It deletes data directories, build artifacts, and environment registry entries, allowing you to completely clean up after an environment is no longer needed or to reuse an environment name.
torrust-tracker-deployer purge <ENVIRONMENT> [OPTIONS]Arguments:
<ENVIRONMENT>- Name of the environment to purge (required)
Options:
--force- Skip confirmation prompt (for automation)--help- Display help information--working-dir <DIR>- Set the working directory (default: current directory)--log-output <OUTPUT>- Logging output mode (default:file-only)file-only: Write logs to file only (production mode)file-and-stderr: Write logs to both file and stderr (development/testing mode)
--log-file-format <FORMAT>- Format for file logging (default:compact)pretty: Pretty-printed output for development (no ANSI codes in files)json: JSON output for production environmentscompact: Compact output for minimal verbosity
--log-stderr-format <FORMAT>- Format for stderr logging (default:pretty)pretty: Pretty-printed output with colors for developmentjson: JSON output for machine processingcompact: Compact output with colors for minimal verbosity
--log-dir <DIR>- Log directory (default:./data/logs)
Purge an environment with confirmation prompt:
torrust-tracker-deployer purge my-environmentYou'll be prompted to confirm:
⚠️ WARNING: This will permanently delete all local data for 'my-environment':
• data/my-environment/ directory
• build/my-environment/ directory
• Environment registry entry
This operation CANNOT be undone!
Are you sure you want to continue? (y/N):
For scripts and automation, use --force to skip the confirmation prompt:
torrust-tracker-deployer purge my-environment --forceOutput:
⏳ [1/3] Validating environment...
⏳ ✓ Done (took 0ms)
⏳ [2/3] Purging local data...
⏳ ✓ Done (took 0ms)
✅ Environment 'full-stack-docs' purged successfully
See detailed progress during purge:
torrust-tracker-deployer purge my-environment --force --log-output file-and-stderrThe purge command supports machine-readable JSON output via the --output-format json flag. This is useful for automation, scripts, and AI agent workflows.
torrust-tracker-deployer purge my-environment --force --output-format jsonOutput (stdout):
{
"environment_name": "my-environment",
"purged": true
}| Field | Type | Description |
|---|---|---|
environment_name |
string | Name of the environment that was purged |
purged |
boolean | Always true when the command exits successfully |
-
purgedis alwaystrueon the success path — purge failures exit with a non-zero code and produce no JSON -
Progress lines (step counters) are written to stderr; only the JSON result is written to stdout
-
When using JSON mode in automation, always pass
--forceto skip the interactive confirmation prompt:torrust-tracker-deployer purge my-environment --force --output-format json
Extract the environment name from the JSON output:
result=$(torrust-tracker-deployer purge my-environment --force --output-format json 2>/dev/null)
env_name=$(echo "$result" | jq -r '.environment_name')
purged=$(echo "$result" | jq -r '.purged')
echo "Purged: $env_name (success=$purged)"The purge command removes:
-
Data Directory (
data/<environment-name>/)- Environment state file (
environment.json) - State history and metadata
- Trace files and logs
- Environment state file (
-
Build Directory (
build/<environment-name>/)- Generated Ansible playbooks
- Generated Docker Compose files
- Generated OpenTofu/Terraform files
- Any other build artifacts
-
Environment Registry Entry
- Removes the environment from the deployer's internal registry
- Allows the environment name to be reused
Important: Purge does NOT destroy infrastructure. You must run destroy first to tear down VMs and resources.
The typical cleanup sequence:
# Step 1: Destroy infrastructure
torrust-tracker-deployer destroy my-env
# Step 2: Purge local data
torrust-tracker-deployer purge my-env --forceAfter purging, you can reuse the environment name:
# Cleanup old environment
torrust-tracker-deployer destroy old-env
torrust-tracker-deployer purge old-env --force
# Create new environment with same name
torrust-tracker-deployer create environment --env-file new-config.json
# (where new-config.json has environmentName: "old-env")Remove local data from a failed deployment:
# If infrastructure was never created or already destroyed
torrust-tracker-deployer purge failed-env --forceAutomate cleanup of multiple environments:
#!/bin/bash
# cleanup-environments.sh
ENVIRONMENTS=("dev-1" "dev-2" "staging-temp")
for env in "${ENVIRONMENTS[@]}"; do
echo "Cleaning up $env..."
# Destroy infrastructure
torrust-tracker-deployer destroy "$env" 2>/dev/null || true
# Purge local data
torrust-tracker-deployer purge "$env" --force
echo "✅ $env cleaned up"
doneIf you destroyed infrastructure manually (outside the deployer):
# Infrastructure already gone, just clean up local data
torrust-tracker-deployer purge my-env --force- After
destroycommand: Normal cleanup workflow - Freeing up disk space: Remove old environment data
- Reusing environment names: Allow name to be used again
- Manual infrastructure removal: Infrastructure destroyed outside deployer
- Failed deployments: Clean up after errors in early stages
- Infrastructure still running: Run
destroyfirst to tear down VMs - Need to keep data for debugging: Purge removes all local state
- Want to resume deployment: Purge makes environment unrecoverable
- Auditing required: Purge removes state history
The purge command is idempotent - you can run it multiple times safely:
- If data is already purged, the command reports an error (environment not found)
- Individual directory removals are idempotent (succeed if already removed)
- Useful in cleanup scripts where you want to ensure complete removal
# First purge succeeds
torrust-tracker-deployer purge my-env --force
# Second purge fails with "environment not found" (expected)
torrust-tracker-deployer purge my-env --force0- Success (environment purged successfully)1- Error (purge failed or environment not found)
Symptom: Purge fails because the environment doesn't exist in the registry.
❌ Purge command failed: Failed to purge environment 'my-env': Environment not found: my-env
Possible Causes:
- Environment was already purged
- Environment was never created
- Wrong environment name
- Wrong working directory
Solution:
# List existing environments
torrust-tracker-deployer list
# Verify working directory
pwd
ls data/
# Use correct environment name from list
torrust-tracker-deployer purge correct-name --forceSymptom: Purge fails due to file permission issues.
❌ Failed to purge environment 'my-env': Failed to remove data directory: Permission denied
Solution:
# Check directory permissions
ls -la data/my-env/
ls -la build/my-env/
# Fix permissions if needed
chmod -R u+w data/my-env/
chmod -R u+w build/my-env/
# Try purge again
torrust-tracker-deployer purge my-env --force
# If still failing, manually remove with sudo
sudo rm -rf data/my-env/ build/my-env/Symptom: Purge fails when trying to read confirmation in non-interactive environment.
❌ Failed during reading user confirmation: cannot read from stdin
Solution:
Use --force flag to skip the interactive prompt:
# In CI/CD, automation, or piped scripts
torrust-tracker-deployer purge my-env --forceSymptom: Purge command succeeds but directories still exist.
Solution:
This shouldn't happen in normal operation. If it does:
# Manually verify what remains
ls -la data/my-env/ 2>/dev/null
ls -la build/my-env/ 2>/dev/null
# Manually remove if necessary
rm -rf data/my-env/ build/my-env/
# Check for file locks
lsof +D ./data/my-env/ 2>/dev/null
lsof +D ./build/my-env/ 2>/dev/nullSymptom: You purged local data but infrastructure still exists.
Important: Purge does NOT destroy infrastructure!
Solution:
# You must destroy infrastructure separately
# This may fail if local state is gone, requiring manual cleanup
# Option 1: If you have infrastructure details
lxc list # Find your container/VM
lxc delete <container-name> --force
# Option 2: Use external tools
# OpenTofu cleanup if you have state files elsewhere
cd /path/to/tofu/state
tofu destroy -auto-approveIf purge fails, check the logs for detailed information:
# View logs
cat data/logs/log.txt
# With verbose output for debugging
torrust-tracker-deployer purge my-env --force \
--log-output file-and-stderr \
--log-stderr-format prettyThe logs will show:
- Which step failed during purge
- File system operation details
- Path information for directories
- Error context and troubleshooting hints
The purge command is destructive and irreversible.
- All local environment data is permanently deleted
- Environment state and history are lost
- Build artifacts and generated files are removed
- Environment name becomes available for reuse
- There is NO recovery mechanism
-
Destroy Infrastructure First
- Always run
destroybeforepurge - Ensures infrastructure is properly torn down
- Prevents orphaned resources
- Always run
-
Backup Important Data
- Save any configuration files you need
- Export environment details with
showcommand - Keep copies of custom templates
-
Double-Check Environment Names
- Use
listcommand to verify environment name - Confirm you're purging the correct environment
- Use tab completion to avoid typos
- Use
-
Understand --force Flag
- Only use
--forcein automation - Interactive mode provides a safety check
- Confirmation prompt prevents accidental purges
- Only use
-
Document Purge Operations
- Keep records of when environments were purged
- Note why the environment was removed
- Track environment lifecycle for auditing
Before purging, consider backing up:
- Environment configuration file (original JSON)
- Generated build artifacts (if needed for reference)
- Environment state information (
showcommand output) - Any custom modifications to generated files
- Trace files and logs (if needed for debugging)
| Aspect | destroy |
purge |
|---|---|---|
| Infrastructure | ✅ Removes VMs, networks | ❌ Does nothing with infrastructure |
| Local Data | ✅ Removes completely | |
| Environment State | ✅ Sets to "Destroyed" | ✅ Removes from registry |
| Name Reuse | ❌ Name remains reserved | ✅ Name becomes available |
| Reversibility | ❌ Completely irreversible | |
| Typical Usage | Tear down running infrastructure | Clean up after destroy |
| Required For | Stopping VMs and freeing resources | Reusing names, freeing disk space |
| Can Run Standalone | ✅ Yes (normal operation) | ✅ Yes (for manual cleanup) |
| Confirmation Prompt | ❌ No (destructive but necessary) | ✅ Yes (unless --force) |
For complete cleanup, always use both commands in order:
# 1. Destroy infrastructure first
torrust-tracker-deployer destroy my-env
# 2. Then purge local data
torrust-tracker-deployer purge my-env --forceThis ensures:
- Infrastructure is properly torn down
- No orphaned resources remain
- Local data is completely removed
- Environment name can be reused
- destroy command - Tear down infrastructure before purging
- show command - View environment details before purging
- list command - See all environments that can be purged
- create command - Create new environments after purging
- Quick Start Guides - Complete deployment workflows