Overview
The TailStack clean scripts (clean.ps1 and clean.sh) are high-performance cleanup utilities designed to completely purge node_modules directories and pnpm-lock.yaml files from your entire monorepo. These scripts employ parallel processing, brutal verification loops, and process killing to handle even the most stubborn file locks.
Key Features
Parallel Execution
Uses system threads (PowerShell Runspaces / Bash xargs) to delete multiple directories simultaneously
Retry Logic
3-phase verification system with automatic retries for locked files
Process Termination
Automatically kills Node.js and VS Code processes that lock files
Interactive Handoff
Optional prompt to immediately run fresh installation after cleanup
How It Works
The clean script operates in three distinct phases:Phase 1: Initial Nuke
- Scanning: Recursively discovers all
node_modulesdirectories andpnpm-lock.yamlfiles - Parallel Deletion: Uses maximum CPU threads (CPU count × 2) to delete targets simultaneously
- Progress Tracking: Displays real-time deletion progress
Phase 2: Brutal Verification Loop
- Survivor Scan: Checks for any remaining
node_modulesdirectories - Process Killing: Terminates Node.js and VS Code processes that may be locking files
- Retry Attempts: Up to 3 retry cycles with forced deletion
- Escalation: Uses OS-level commands (
cmd /c rdon Windows,rm -rfon Unix)
Phase 3: Handoff
- Completion Report: Shows total execution time
- Interactive Prompt: Asks if you want to run a fresh installation
- Auto-Launch: Optionally triggers
install.ps1orinstall.sh
Usage
- PowerShell
- Shell
First run? You may need to enable script execution:
Performance Characteristics
Execution Speed
Execution Speed
Typical Performance:
- Small monorepo (5-10 packages): 2-5 seconds
- Medium monorepo (20-50 packages): 5-15 seconds
- Large monorepo (100+ packages): 15-45 seconds
- Number of
node_modulesdirectories - Total file count in dependencies
- Disk I/O speed (SSD vs HDD)
- Antivirus interference (Windows)
Parallel Processing
Parallel Processing
The script calculates optimal thread count:Example:
- 4-core CPU (8 threads) → 16 parallel deletions
- 8-core CPU (16 threads) → 32 parallel deletions
- 16-core CPU (32 threads) → 64 parallel deletions
Retry Mechanism
Retry Mechanism
If files survive initial deletion:
- Retry 1: Kill Node/VS Code processes, re-delete
- Retry 2: Force deletion with OS commands
- Retry 3: Final attempt with all escalations
Output Examples
- Successful Cleanup
- With Retries
Troubleshooting
Common Issues
Permission Denied Errors
Permission Denied Errors
Windows:Linux/macOS:
Files Still Locked After 3 Retries
Files Still Locked After 3 Retries
Manual intervention steps:
- Close all editors and terminals
- Windows: Use Process Explorer to find locking processes
- Linux/macOS: Use
lsofto identify locks: - Reboot as last resort (releases all file locks)
Antivirus Interference (Windows)
Antivirus Interference (Windows)
Windows Defender or third-party antivirus may slow deletion:
- Add exclusion for your project directory
- Temporarily disable real-time scanning
- Check antivirus logs for quarantined files
What Gets Deleted
The script targets:**/node_modules/- All dependency directories (nested included)**/pnpm-lock.yaml- Lock files at any depth
package.jsonfiles (metadata preserved).npmrcand.pnpmfile.cjs(configuration preserved)- Source code and other project files
Integration with Install Script
The clean script seamlessly integrates with the install script:Pressing Y at the prompt automatically launches the install script for a fresh dependency installation.
Technical Implementation
PowerShell Architecture
Key Components
Shell Script Architecture
Key Components
Best Practices
Related Documentation
Install Script
Learn about parallel installation with load monitoring
Custom Scripts
Create your own automation scripts for the monorepo