Fast Delete Large Directory Trees - Version 3.1.0
A high-performance utility for rapidly deleting extremely large directory trees where standard rm -rf is too slow or inefficient. Uses rsync --delete from an empty temporary directory for optimal performance.
- High-speed deletion using rsync with empty source directory technique
- Filesystem-specific optimizations automatically detects and optimizes for XFS, Btrfs, ext4, and other filesystems
- Safe by default - dry-run mode enabled by default to preview operations
- I/O priority control with ionice/nice for system load management (levels 0-3)
- Timeout capability to prevent runaway processes (supports minutes/hours notation)
- Mount point protection prevents accidental deletion of mounted filesystems
- Memory optimization drops kernel caches before deletion (non-dry-run mode only)
- Multiple directory support delete multiple directories in a single command
- Wait for rsync option to queue deletions when other rsync processes are running
- Verbose rsync output optional detailed progress reporting
- Standardized output icons visual status indicators (◉ info, ▲ warning, ✓ success, ✗ error)
- Root privileges or non-interactive sudo access
- rsync command-line tool
- bash shell (version 4.0+)
- ionice and nice (optional, for I/O priority control)
- timeout command (optional, for time limits)
curl -fsSL https://raw.githubusercontent.com/Open-Technology-Foundation/nukedir/main/install.sh | sudo bashOr with wget:
wget -qO- https://raw.githubusercontent.com/Open-Technology-Foundation/nukedir/main/install.sh | sudo bash-
Clone this repository:
git clone https://github.com/Open-Technology-Foundation/nukedir.git cd nukedir -
Run the installer:
sudo ./install.sh
nukedirscript →/usr/local/bin/nukedir- Bash completion →
/etc/bash_completion.d/nukedir
sudo ./install.sh --uninstallnukedir --versionnukedir [OPTIONS] dirname [dirname ...]# Dry run (default - shows what would be deleted without actually deleting)
nukedir /path/to/large/directory
# Actually delete the directory (requires explicit --notdryrun)
nukedir --notdryrun /path/to/large/directory
# Delete with quiet mode (minimal output)
nukedir -qN /path/to/directory# Set a 4-hour timeout for extremely large directories
nukedir -T 4h --notdryrun /path/to/huge/directory
# Use highest I/O priority (1) for faster deletion, verbose rsync output
nukedir -Nri 1 /path/to/large/directory
# Delete multiple directories with high priority, quiet mode
nukedir -wqN -i 1 /backups/old/set-{01..04}
# Wait for other rsync processes to finish before starting
nukedir --wait-for-rsync -N /path/to/directory
# Verbose rsync output to monitor progress
nukedir -rN /path/to/directory| Option | Long Form | Description | Default |
|---|---|---|---|
-n |
--dryrun |
Dry run mode (no actual deletion) | Enabled |
-N |
--notdryrun |
Execute actual deletion | Disabled |
-T |
--timeout N |
Maximum runtime (e.g., 2m, 4h) | No timeout |
-i |
--ionice 0-3 |
I/O priority (0=none, 1=highest, 3=lowest) | 0 |
-w |
--wait-for-rsync |
Wait for other rsync processes | Disabled |
-r |
--rsync-verbose |
Extra verbose rsync output | Disabled |
-v |
--verbose |
Verbose output | Enabled |
-q |
--quiet |
Suppress output (recommended with -N) | Disabled |
-V |
--version |
Display version and exit | - |
-h |
--help |
Display help and exit | - |
- Default dry-run mode - Must explicitly use
-Nor--notdryrunto delete - Root directory protection - Cannot run from or target root directory (
/) - Mount point protection - Refuses to delete mount points
- Directory validation - Verifies targets exist and are directories
- Process warnings - Warns if other rsync processes are running
nukedir uses an efficient technique for deleting large directory trees:
-
Creates an empty temporary directory (BITBIN):
- Preferably in
/run(tmpfs) for best performance - Falls back to
/tmpif/runis not available or not tmpfs
- Preferably in
-
Detects the filesystem type using
df -PTto optimize deletion strategy -
Applies filesystem-specific optimizations:
- XFS: Uses
--delete-duringfor optimal performance with XFS's allocation groups - Btrfs: Uses
--delete-delaywith--preallocatefor better B-tree handling - Other filesystems (ext4, etc.): Uses
--delete-beforewith--no-inc-recursiveand--inplace
- XFS: Uses
-
Memory optimization (non-dry-run mode only):
- Drops kernel caches before deletion using
sync && echo 3 > /proc/sys/vm/drop_caches - This frees memory and improves deletion performance
- Drops kernel caches before deletion using
-
Uses rsync with --delete to synchronize the target directory with the empty BITBIN directory, effectively deleting all contents
-
Removes the empty target directory with
rmdirafter successful deletion
This approach is significantly faster than rm -rf for directories containing millions of files, particularly on filesystems like XFS and ext4 where rsync's batched deletion is more efficient than recursive unlinking.
nukedir uses standardized visual status indicators:
- ◉ Info - General information messages
- ▲ Warning - Important warnings that don't stop execution
- ✓ Success - Successful operation completion
- ✗ Error - Error messages
These icons are color-coded when running in a terminal (info=cyan, warning=yellow, success=green, error=red).
- Use ionice level 1 (
-i 1) for fastest deletion on dedicated systems - Use ionice level 3 (
-i 3) to minimize impact on other processes - Set appropriate timeouts (
-T) for very large directories to prevent hanging - Use quiet mode (
-q) with-Nto reduce output overhead - Consider filesystem type - XFS and ext4 typically perform better than Btrfs for large deletions
- Ensure sufficient RAM - While the script clears kernel caches, rsync still needs memory to operate
- Use /run for BITBIN - The script automatically prefers tmpfs-backed /run for better performance
- "Requires root" error: Run with
sudoor as root user - "Cannot execute from root directory": Change to a different directory before running
- "Cannot delete a mount point": Unmount the filesystem first or delete contents only
- Timeout errors: Increase timeout value with
-Toption (e.g.,-T 8h) - Memory issues: Script automatically drops caches in non-dry-run mode, but ensure sufficient RAM for rsync
- Other rsync processes running: Use
-wto wait for them to finish, or proceed with caution
nukedir includes a comprehensive test suite using BATS (Bash Automated Testing System).
tests/
├── unit/ # Function-level tests (basic, validation, filesystem)
├── integration/ # End-to-end deletion tests
├── safety/ # Security and protection mechanism tests
├── performance/ # Performance benchmarks (optional)
├── fixtures/ # Test data generators
├── helpers/ # Test utilities and helpers
└── run-all-tests.sh # Main test runner
Prerequisites:
# Install BATS
sudo apt-get install bats # Ubuntu/Debian
brew install bats-core # macOS
# Install shellcheck (recommended)
sudo apt-get install shellcheckRun all tests:
sudo ./tests/run-all-tests.shRun specific test categories:
sudo ./tests/run-all-tests.sh --unit # Unit tests only
sudo ./tests/run-all-tests.sh --integration # Integration tests only
sudo ./tests/run-all-tests.sh --safety # Safety tests only
sudo ./tests/run-all-tests.sh --shellcheck # Shellcheck onlyRun performance tests (skipped by default):
PERF_TESTS=1 sudo ./tests/run-all-tests.sh --performanceRun stress tests (manual only):
STRESS_TESTS=1 sudo ./tests/run-all-tests.sh --performance-
Unit Tests (tests/unit/)
- Basic functionality (version, help, options)
- Input validation (paths, arguments)
- Filesystem detection and optimization
-
Integration Tests (tests/integration/)
- Actual deletion operations (controlled environment)
- Multiple directory handling
- ionice/timeout integration
- Dry-run vs. actual deletion
-
Safety Tests (tests/safety/)
- Root directory protection
- Mount point protection
- Permission validation
- Default dry-run enforcement
-
Performance Tests (tests/performance/)
- Deletion speed benchmarks
- Filesystem comparison
- ionice level impact
- Memory and CPU usage
# Quick functionality test (safe)
./nukedir --dryrun /path/to/test/dir
# Lint check
shellcheck nukedir
# Performance test with timeout
./nukedir -T 4h --dryrun /path/to/large/dirTests run automatically on GitHub Actions for:
- Every push to main/develop branches
- All pull requests
- Multiple Ubuntu versions (20.04, 22.04, 24.04)
See .github/workflows/test.yml for CI configuration.
The -i / --ionice option controls system resource usage:
- 0 (default): No I/O priority adjustment
- 1: Highest priority (
nice -n -19+ionice -c1 -n0) - fastest deletion, may impact other processes - 2: Medium priority
- 3: Lowest priority - minimal impact on other processes, slower deletion
Contributions are welcome! Please:
- Run the test suite before submitting PRs:
sudo ./tests/run-all-tests.sh
- Run shellcheck to ensure code quality:
shellcheck nukedir
- Test changes in dry-run mode first for safety
- Add tests for new features or bug fixes
- Follow existing code style (2-space indentation, BCS-compliant bash)
- Update version number in script header for significant changes
- Update README if adding new features or changing behavior
This project is licensed under the GNU General Public License v3.0 - see the LICENSE file for details.
Part of the Open Technology Foundation toolkit.