Skip to main content
pypurge logo

pypurge

Safe & Powerful Python Project Cleaner

PyPI version Python Wheel Release Build status Codecov Test Coverage Code style: black Ruff Security Downloads OS License: MIT


pypurge is a production-grade Python cleanup utility designed to safely remove auto-generated files, caches, virtualenv leftovers, test artifacts, and temporary files — without putting your system at risk.

Think of it as a precision broom for Python projects. No more find . -name __pycache__ -delete or risky scripts.


⚡ Quick Start (The "5-Minute Rule")

Prerequisites

  • Python 3.8+
  • No other external dependencies required.

Installation

pip install pypurge

Run

Clean your current project interactively:

pypurge

Demo

# Preview what would be deleted without touching anything
pypurge --preview

# Clean everything, including virtual environments, older than 7 days
pypurge --clean-venv --older-than 7

✨ Features

Core

  • Targeted Cleanup: Smartly handles __pycache__, .pytest_cache, build/, dist/, .egg-info, and more.
  • Virtualenv Purge: Optional cleaning of virtual environments (--clean-venv).
  • Smart Preview: Shows detailed counts, groups, and disk usage before you confirm deletion.
  • Shell Completions: Native auto-completion for bash, zsh, and fish.

Performance & Safety

  • 🛡️ Safety-first Design: Prevents accidental root-level deletion (/, ~) and protects system directories.
  • Atomic Backups: Create a zip backup with SHA256 manifest before cleaning (--backup).
  • Concurrency Safety: Stale lock & lockfile protection to avoid multi-process conflicts.
  • Gitignore Awareness: Respects .gitignore rules to avoid cleaning untracked files (--no-gitignore to disable).

Advanced

  • Age-based Filtering: Delete only items older than N days (--older-than).
  • Configuration Wizard: Easily setup exclusions with pypurge --init.
  • CI/CD Ready: Supports non-interactive modes (--yes, --quiet, --log-format json).

🛠️ Configuration

You can configure pypurge using CLI arguments or a .pypurge.json file.

CLI Arguments

Flag Description Default
root... Directories to clean. .
-p, --preview Preview targets without deleting. False
-y, --yes Skip interactive confirmation. False
-q, --quiet Suppress output except errors. False
--clean-venv Include .venv, venv folders. False
--exclude <pattern> Exclude glob or regex (re:...). None
--older-than <days> Only target items older than N days. 0
--force Force deletion (chmod if needed). False
--backup Create a .zip backup before delete. False
--config <path> Path to .pypurge.json. Auto-detect
--init Run configuration wizard. False
--completions <shell> Generate shell completions. None

Configuration File (.pypurge.json)

Create a .pypurge.json file in your project root for persistent settings.

{
  "exclude_dirs": [".git", "node_modules"],
  "exclude_patterns": ["re:.*migrations.*", "data/"],
  "dir_groups": {
    "CustomData": ["temp_run/", "scratch/"]
  }
}

🏗️ Architecture

pypurge follows a modular architecture for safety and maintainability.

src/pypurge/
├── cli.py             # Entry point
└── modules/
    ├── safety.py      # Root/System protection logic
    ├── scan.py        # File scanning & pattern matching
    ├── deletion.py    # Safe removal operations
    ├── locking.py     # Process locking mechanism
    ├── backup.py      # Atomic backup creation
    ├── config.py      # JSON schema validation
    └── ui.py          # Rich terminal output

Data Flow:

  1. CLI: Parses args and loads config.
  2. Safety: Validates target root (blocks /, ~).
  3. Locking: Acquires .pypurge.lock.
  4. Scan: Walks directory tree -> Filters by rules/gitignore -> Groups targets.
  5. UI: Displays preview & asks confirmation.
  6. Backup: (Optional) Archives targets to zip.
  7. Deletion: Removes files/dirs.
  8. Cleanup: Releases lock.

🐞 Troubleshooting

Issue Possible Cause Solution
"Operation not permitted" File permissions or locked file. Try --force or check file owner.
"Lock file is stale" Previous run crashed or is running. Remove .pypurge.lock or wait.
"Refusing to run on dangerous root" You are running on / or ~. Use --allow-broad-root (Caution!).
No files found .gitignore might be hiding them. Try --no-gitignore.

Debug Mode: Use --log-format text (default) or --log-format json with --log-file pypurge.log to inspect operations.


🤝 Contributing

We welcome contributions! Please see CONTRIBUTING.md for details on how to set up your development environment and submit Pull Requests.

To run tests locally:

tox

🗺️ Roadmap

We are constantly improving pypurge. Check out our ROADMAP.md for upcoming features, including:

  • Enhanced AI-based clutter detection.
  • Plugin system for custom cleaners.
  • Deeper IDE integrations.

License

MIT © Dhruv

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pypurge-5.0.0.tar.gz (42.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pypurge-5.0.0-py3-none-any.whl (24.2 kB view details)

Uploaded Python 3

File details

Details for the file pypurge-5.0.0.tar.gz.

File metadata

  • Download URL: pypurge-5.0.0.tar.gz
  • Upload date:
  • Size: 42.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pypurge-5.0.0.tar.gz
Algorithm Hash digest
SHA256 c87e07a512836a944f21cc43b213d43dc907d8a27d91c03ab21eff0d60406caa
MD5 41375fae5e46f86aea70ea79e0893fca
BLAKE2b-256 41da75957a4320b783bf0d500e6809f7f72b83844c664a6cdfa471b39220f642

See more details on using hashes here.

Provenance

The following attestation bundles were made for pypurge-5.0.0.tar.gz:

Publisher: publish.yml on dhruv13x/pypurge

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pypurge-5.0.0-py3-none-any.whl.

File metadata

  • Download URL: pypurge-5.0.0-py3-none-any.whl
  • Upload date:
  • Size: 24.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pypurge-5.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b3b3f92ede6cc0b4c191f9e1d71d4d8e6e28206500fdb0d96ce165240d07bb43
MD5 7906882d3cff3213516082fe92c711d5
BLAKE2b-256 b1c8828f48eaa3771ae495f3d38ec93810d1a4a7f5ec555e150fad1ed44de27e

See more details on using hashes here.

Provenance

The following attestation bundles were made for pypurge-5.0.0-py3-none-any.whl:

Publisher: publish.yml on dhruv13x/pypurge

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

5.0.0 This release

2 files

4.0.1

2 files

4.0.0

2 files

3.0.2

2 files

3.0.1

2 files

1.0.0

2 files

0.1.0

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page