Skip to content

elemeng/spacefree

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

30 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿš€ spacefree

๐Ÿš€ Ultra-fast, storage-aware file deletion CLI tool with trash support

Rust Crates.io License: MIT


โœจ Features

Feature Description
๐Ÿš€ Blazing Fast Async parallel scanning & deletion with adaptive storage optimization
๐Ÿง  Storage-Aware Auto-detects HDD/SSD and optimizes for sequential or parallel access
๐Ÿ›ก๏ธ Safe Trash mode, dry-run preview, symlink protection, root directory guards
๐ŸŽฏ Flexible Glob patterns, size filters, age filters, exclusion rules
๐Ÿ“ Batch Ready Accept directories, files, or path list files
๐Ÿ“ Logging Optional NDJSON log of all deleted items
๐Ÿงน Smart Auto-skip non-existent paths, deduplicates, graceful Ctrl+C handling
๐Ÿ‘๏ธ Verbose Optional detailed file listing for visibility

๐Ÿ–ฅ๏ธ Platform Support

Platform Trash Support Storage Detection Status
๐Ÿง Linux โœ… Yes โœ… HDD/SSD via /sys/block โœ… Fully supported
๐ŸŽ macOS โœ… Yes โœ… SSD via diskutil โœ… Fully supported
๐ŸชŸ Windows โœ… Yes โœ… SSD via WMI/TRIM โœ… Fully supported

๐Ÿ“ฆ Installation

From crates.io

cargo install spacefree

Two binaries are installed:

  • spacefree - full name
  • spa - short alias

From source

git clone https://github.com/elemeng/spacefree
cd spacefree
cargo install --path .

Or build and run directly:

cargo build --release
./target/release/spa --help

๐ŸŽฎ Quick Start

Basic Usage

# ๐Ÿ—‘๏ธ  Delete ALL files from directories (be careful!)
$ spa J12 J13 J14

# ๐Ÿ‘€ Preview before delete (dry run - recommended)
$ spa J12 --dry-run

# ๐ŸŽฏ Delete only specific file types
$ spa J12 -g "*.log"

# โ™ป๏ธ  Move to system trash (safer)
$ spa J12 --trash

# ๐Ÿ“‹ Log deleted items to file
$ spa J12 -l                    # Auto-named log (spacefree_0001.log)
$ spa J12 -l /path/to/log.json  # Custom log path

๐Ÿ“‹ Usage Examples

Filter by File Size

# Only files >= 10 megabytes
$ spa J12 --min-size 10M

# Files between 10MB and 1GB
$ spa J12 --min-size 10M --max-size 1G

# Supported units: B (bytes), K/KB, M/MB, G/GB, T/TB
$ spa J12 --min-size 1G      # 1 gigabyte
$ spa J12 --min-size 512k    # 512 kilobytes

Filter by File Age

# Files older than 7 days
$ spa J12 --min-age 7d

# Files newer than 1 hour
$ spa J12 --max-age 1h

# Files between 1 week and 1 month old
$ spa J12 --min-age 1w --max-age 1M

# Supported units: s/sec, m/min, h/hour, d/day, w/week, M/month, y/year
$ spa J12 --min-age 2w       # 2 weeks
$ spa J12 --min-age 3m       # 3 months
$ spa J12 --min-age 1y       # 1 year

File Patterns (Glob)

By default, all files (**/*) are selected. Use -g to filter:

# Delete only .log files
$ spa J12 -g "*.log"

# Multiple patterns
$ spa J12 -g "**/*.{tmp,cache}"

# Exclude certain patterns
$ spa J12 -g "*.txt" --exclude "**/important.txt"

Delete Directories

# Delete empty directories as well as files
$ spa J12 --dirs

# Directories are only deleted when empty (after files are removed)

Safety Options

# Follow symbolic links (disabled by default for safety)
$ spa J12 --follow-symlinks

# Delete root directory (requires both flags for safety)
$ spa /path --delete-root-dir -y

Batch Processing from File

Create a jobs.txt file:

J12
J13, J14
J15

Then run:

spa --path-list-file jobs.txt

Or mix directories and path list files:

spa J12 --path-list-file jobs.txt J20

Storage-Aware Optimization

# Auto-detect storage type and optimize (default)
$ spa J12
# Output: Storage: Ssd โ†’ parallelism: 16

# Force HDD-optimized sequential deletion
$ spa J12 -p 1

# Force high parallelism for SSD
$ spa J12 -p 32

Parallel Workers

# Use 16 parallel workers (default: auto-detect based on storage)
$ spa J12 -p 16

# Sequential processing (best for HDDs)
$ spa J12 -p 1

Skip Confirmation

# Auto-confirm deletion (use with caution!)
$ spa J12 --yes

Verbose Mode

# Show all files to be deleted
$ spa J12 -v --dry-run

Logging

# Auto-generate log filename (spacefree_0001.log, etc.)
$ spa J12 -l

# Specify custom log path
$ spa J12 -l /var/log/deletions.json

# Log format: NDJSON (one JSON object per line)
# {"path":"/path/to/file","is_dir":false,"deleted_at":1234567890}

๐Ÿ› ๏ธ Command Reference

Usage: spa [OPTIONS] <PATHS>...

Arguments:
  <PATHS>...  Paths to scan - directories or files to delete

Options:
  -g, --glob <PATTERN>       Glob pattern for files [default: **/*]
      --exclude <PATTERN>    Glob pattern to exclude
      --min-size <SIZE>      Minimum file size (e.g., 10k, 5M, 1G) [default: 0]
      --max-size <SIZE>      Maximum file size (e.g., 10k, 5M, 1G)
      --min-age <AGE>        Minimum file age (e.g., 1d, 2w, 3m, 1y)
      --max-age <AGE>        Maximum file age (e.g., 1d, 2w, 3m, 1y)
      --trash                Move to system trash instead of permanent delete
      --dry-run              Preview what would be deleted
  -y, --yes                  Skip confirmation prompt
      --delete-root-dir      Allow deleting root directory (requires -y)
  -p, --parallelism <N>      Number of workers (0 = auto-detect) [default: 0]
  -v, --verbose              Show all files to be deleted
      --dirs                 Delete empty directories as well
      --follow-symlinks      Follow symbolic links (disabled by default)
      --path-list-file <FILE>  File containing paths to process
  -l, --log [<PATH>]         Log deleted items (auto-named or specify path)
  -h, --help                 Print help
  -V, --version              Print version

๐Ÿง  Storage-Aware Optimization

spacefree automatically detects your storage type and optimizes deletion strategy:

Storage Parallelism Strategy Why
HDD 1 worker Sorted by path, sequential access Minimizes seek time
SSD num_cpus ร— 4 Streaming, high parallelism Maximizes queue depth
Unknown num_cpus ร— 2 Balanced Safe default

Detection Methods

  • Linux: Reads /sys/block/*/queue/rotational
  • macOS: Uses diskutil info to check "Solid State" property
  • Windows: Queries WMI Win32_DiskDrive or checks TRIM support

โš ๏ธ Safety Features

  1. Always use --dry-run first to preview what will be deleted
  2. By default, ALL files are selected - use -g to filter by pattern
  3. Use --trash for safer deletion (can be recovered from system trash)
  4. Symlink protection - Symbolic links are NOT followed by default (use --follow-symlinks to enable)
  5. Root directory guard - Requires both --delete-root-dir and -y to delete /
  6. Graceful interruption - Press Ctrl+C to stop safely after current operations
  7. Empty directory check - Directories only deleted when truly empty

Confirmation

When deleting without --yes, you'll be prompted:

Type exactly YES to continue:

๐Ÿค Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Development Setup

# Clone the repository
git clone https://github.com/elemeng/spacefree
cd spacefree

# Run tests
cargo test

# Run with debug output
RUST_LOG=info cargo run --bin spa -- J12 --dry-run

# Build release binary
cargo build --release

# Check code style
cargo clippy

# Format code
cargo fmt

Project Structure

src/
โ”œโ”€โ”€ main.rs      # Entry point & CLI orchestration
โ”œโ”€โ”€ cli.rs       # CLI parsing & argument definitions
โ”œโ”€โ”€ config.rs    # DeleteConfig & ScanResult types
โ”œโ”€โ”€ scan.rs      # Directory scanning & path collection
โ”œโ”€โ”€ delete.rs    # Deletion pipeline & trash worker
โ”œโ”€โ”€ storage.rs   # HDD/SSD detection & optimization
โ”œโ”€โ”€ log.rs       # DeletedItem logging & LogMode
โ””โ”€โ”€ error.rs     # DeleterError type

Bug Reports & Feature Requests

Please use GitHub Issues to report bugs or request features.

When reporting bugs, please include:

  • Operating system and version
  • Rust version (rustc --version)
  • Steps to reproduce
  • Expected vs actual behavior

๐Ÿ“„ License

MIT ยฉ elemeng


Made with โ˜• and ๐Ÿฆ€

About

High-performance file deletion CLI tool with system trash support!

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages