The destroy command removes all infrastructure and resources for a deployment environment. It safely tears down virtual machines, networks, and related resources created during provisioning.
torrust-tracker-deployer destroy <ENVIRONMENT>Arguments:
<ENVIRONMENT>- Name of the environment to destroy (required)
Options:
--help- Display help information--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)
Destroy an environment:
torrust-tracker-deployer destroy my-environmentWith verbose logging to see progress:
torrust-tracker-deployer destroy my-environment --log-output file-and-stderrThe destroy command removes:
-
Virtual Machine Infrastructure
- LXD containers or VMs
- Network interfaces and bridges
- Allocated IP addresses
-
Local State Files
- Environment data directory (
data/<environment-name>/) - Build artifacts (
build/<environment-name>/) - OpenTofu state files
- Environment data directory (
-
Environment State
- Updates environment state to
Destroyed - Preserves state history for auditing
- Updates environment state to
Remove a test environment after validation:
# Note: Only destroy command is currently implemented
torrust-tracker-deployer destroy test-envClean up after a failed deployment:
torrust-tracker-deployer destroy failed-envAutomate cleanup of temporary environments:
#!/bin/bash
# cleanup-old-environments.sh
ENVIRONMENTS=("dev-1" "dev-2" "staging-temp")
for env in "${ENVIRONMENTS[@]}"; do
echo "Destroying $env..."
torrust-tracker-deployer destroy "$env"
doneQuickly remove an environment in case of issues:
torrust-tracker-deployer destroy emergency-env --log-output file-and-stderrThe destroy command is idempotent - you can run it multiple times safely:
- If infrastructure is already destroyed, the command succeeds without error
- Running destroy twice on the same environment won't cause issues
- Useful in automation scripts where you want to ensure cleanup
# Safe to run multiple times
torrust-tracker-deployer destroy my-env
torrust-tracker-deployer destroy my-env # Still succeeds0- Success (infrastructure destroyed successfully)1- Error (destruction failed)
Symptom: The destroy command seems to hang or take a very long time.
Solution:
-
Check if OpenTofu is waiting for resources:
cd build/tofu/lxd tofu show -
Manually check LXD containers:
lxc list
-
Stop hung containers manually if needed:
lxc stop <container-name> --force lxc delete <container-name>
Symptom: Destroy command fails but some resources are removed.
Solution:
-
Run destroy again (it's idempotent):
torrust-tracker-deployer destroy my-env
-
If it continues to fail, manually clean up using OpenTofu:
cd build/tofu/lxd tofu destroy -auto-approve -
Remove leftover local directories:
rm -rf data/my-env build/my-env
Symptom: Destroy fails with OpenTofu errors.
Error: OpenTofu command failed: resource still in use
Solution:
-
Check OpenTofu state for locked resources:
cd build/tofu/lxd tofu state list -
Unlock any locked resources:
tofu force-unlock <lock-id>
-
Try destroying again:
torrust-tracker-deployer destroy my-env
-
If the issue persists, manually inspect and remove resources:
lxc list lxc delete <container-name> --force
Symptom: Infrastructure destroyed but local files remain.
Error: Failed to clean up state files at 'data/my-env': Permission denied
Solution:
-
Check file permissions:
ls -la data/my-env
-
Manually remove directories with appropriate permissions:
sudo rm -rf data/my-env build/my-env
-
Verify cleanup:
ls data/ build/
Symptom: Destroy fails because resources are in use.
Solution:
-
Stop any running applications first
-
Ensure no active SSH connections to the VM
-
Check for dependent resources:
lxc list lxc network list
-
Manually stop and remove conflicting resources
If destroy fails, check the logs for detailed information:
# View logs
cat data/logs/log.txt
# With pretty format for debugging
torrust-tracker-deployer destroy my-env \
--log-output file-and-stderr \
--log-stderr-format prettyThe logs will show:
- Which step failed during destruction
- Detailed error messages from OpenTofu
- Resource cleanup progress
- State transition information
- All data on the virtual machine will be permanently deleted
- Application data, logs, and configurations will be lost
- State files and build artifacts will be removed
- There is no built-in backup mechanism
Best Practices:
- Backup Important Data: Always backup critical data before destroying
- Double-Check Environment Names: Verify you're destroying the correct environment
- Test in Non-Production First: Practice destruction workflow in test environments
- Document Destruction: Keep records of when and why environments were destroyed
Always backup important data before destroying:
# Example: Backup application data
lxc exec my-env -- tar czf /tmp/backup.tar.gz /opt/torrust
lxc file pull my-env/tmp/backup.tar.gz ./my-env-backup.tar.gz
# Now safe to destroy
torrust-tracker-deployer destroy my-envIn scripts, add confirmation prompts:
#!/bin/bash
read -p "Destroy environment '$1'? (yes/no): " confirm
if [ "$confirm" = "yes" ]; then
torrust-tracker-deployer destroy "$1"
else
echo "Destruction cancelled"
fiFor production environments:
- Require manual approval in automation workflows
- Enable audit logging to track destruction events
- Implement backup policies before any destruction
- Use separate credentials for destroy operations
- Document destruction procedures in runbooks
Example GitHub Actions workflow:
name: Cleanup Test Environments
on:
schedule:
- cron: '0 2 * * *' # Run at 2 AM daily
jobs:
cleanup:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup tools
run: |
# Install OpenTofu, LXD, etc.
- name: Destroy old environments
run: |
for env in test-1 test-2 staging-temp; do
echo "Destroying $env..."
torrust-tracker-deployer destroy "$env" || true
done#!/bin/bash
# cleanup-environments.sh
set -euo pipefail
ENVIRONMENTS_TO_DESTROY=(
"dev-feature-1"
"dev-feature-2"
"test-temp"
)
for env in "${ENVIRONMENTS_TO_DESTROY[@]}"; do
echo "═══════════════════════════════════════════"
echo "Destroying environment: $env"
echo "═══════════════════════════════════════════"
if torrust-tracker-deployer destroy "$env"; then
echo "✓ Successfully destroyed $env"
else
echo "✗ Failed to destroy $env"
# Continue with other environments
fi
echo ""
done
echo "Cleanup complete!"After running destroy, verify complete cleanup:
# List all containers
lxc list
# List all networks
lxc network list
# List all storage pools
lxc storage list# Verify data directory is removed
ls -la data/
# Verify build directory is removed
ls -la build/
# Check OpenTofu state
ls -la build/tofu/lxd/The environment state should show as Destroyed:
# Check state file (if preserved)
cat data/state.json
# Output should show:
# {
# "state": "Destroyed",
# "environment": "my-env",
# ...
# }- Command Index - Overview of all commands
- Logging Guide - Configure logging output and formats
- E2E Testing Guide - How destroy is tested in E2E tests