Safe Python project cleanup utility — purge caches, build artifacts, test leftovers, and env clutter with smart protection.
Project description
🧹 pypurge — Safe & Powerful Python Project Cleaner
pypurge is a production-grade Python cleanup utility designed to safely remove auto-generated files, caches, virtualenv leftovers, test artifacts, temporary files, and clutter — 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 — clean confidently, with safety rails.
✅ Key Features
- 🔐 Safety-first design — prevents accidental root-level deletion
- 🎯 Python-specific cleanup
__pycache__/,*.pyc,.pytest_cache/,.mypy_cache/,.ruff_cache/,build/,dist/, etc.
- 🧠 Smart preview mode — shows counts, groups & disk usage before deleting
- 🪪 Stale lock & lockfile protection — avoids multi-process conflicts
- 🕒 Age-based filtering — delete only items older than N days
- 📦 Atomic backup mode — zip backup with SHA256 manifest
- 🧪 Cleans testing & packaging leftovers
- 🧹 Optional virtualenv purge
- 💬 Colored interactive interface (or JSON for automation)
- 🛑 Root & dangerous directory protection
- ⚙️ Configurable via JSON (
.pypurge.json) - 🤖 Works safely in CI & scripts
📦 Installation
pip install pypurge
Or in development mode:
pip install -e .
---
🚀 Usage
Clean current project interactively
pypurge
Preview everything — no deletions
pypurge --preview
Clean without prompt (CI-safe)
pypurge --yes
Clean a specific folder
pypurge myproject/
Exclude files or directories
pypurge --exclude "*.log" --exclude "re:.*tmp.*"
Force deletion of stubborn files
pypurge --force
Run in non-interactive (quiet) mode
pypurge --quiet
Backup before deleting 🛟
pypurge --backup
Clean virtual environments too
pypurge --clean-venv
Delete only files older than 7 days
pypurge --older-than 7
Allow root / system scans (⚠️ expert mode)
pypurge --allow-root --allow-broad-root
---
✨ Example Output
=== Preview: grouped cleanup summary for .
Group Items Size Paths (truncated)
----------------------------------------------------------------------
Python Caches 84 12.4MB
Testing/Linting/... 36 4.2MB
Build/Packaging 12 2.1MB
📁 Python Caches — 84 items, 12.4MB
src/app/__pycache__/ — 340KB
tests/__pycache__/ — 290KB
...
... and 60 more
---
### ⚙️ Advanced Usage & Configuration
While `pypurge` works great out of the box, you can fine-tune its behavior with command-line arguments or a configuration file.
#### CLI Arguments
| Argument | Short | Description | Default |
| ------------------------- | ----- | ------------------------------------------------------------------------ | ------------------------ |
| `root...` | | Directories to clean. | `.` (current directory) |
| `--preview` | `-p` | Preview targets without deleting anything. | `False` |
| `--yes` | `-y` | Skip the interactive confirmation prompt. | `False` |
| `--quiet` | `-q` | Suppress all output except for critical errors. | `False` |
| `--clean-venv` | | Include virtual environment directories (`.venv`, `venv`, etc.) in the scan. | `False` |
| `--exclude <pattern>` | | Exclude files/directories matching a glob or `re:` pattern. | (none) |
| `--older-than <days>` | | Only target files older than `N` days. | `0` (all ages) |
| `--age-type <type>` | | Choose age metric: `mtime` (modified), `atime` (accessed), `ctime` (created). | `mtime` |
| `--force` | | Attempt to `chmod` files to ensure deletion. | `False` |
| `--backup` | | Create a `.zip` backup of all targets before deletion. | `False` |
| `--backup-dir <path>` | | Specify a directory to store backups. | (root of scan) |
| `--backup-name <name>` | | Set a reproducible base name for backup archives. | (auto-generated) |
| `--delete-symlinks` | | Also delete symbolic links (the link itself, not the target). | `False` |
| `--config <path>` | | Path to a `.pypurge.json` configuration file. | (auto-detect in root) |
| `--allow-broad-root` | | **DANGEROUS**: Allow running in broad directories like `/` or `$HOME`. | `False` |
| `--allow-root` | | **DANGEROUS**: Allow running as the `root` user. | `False` |
| `--lockfile <name>` | | Name of the lockfile to prevent concurrent runs. | `.pypurge.lock` |
| `--lock-stale-seconds <N>`| | Time in seconds before a lock is considered stale. | `86400` (24 hours) |
| `--log-file <path>` | | Path to a file for logging output. | (none) |
| `--log-format <format>` | | Log format: `text` or `json`. | `text` |
| `--no-color` | | Disable colored terminal output. | `False` |
| `--version` | `-v` | Show the application version and exit. | `False` |
#### Configuration File
You can create a `.pypurge.json` file in the root of your project to define custom patterns and exclusions:
```json
{
"exclude_patterns": ["re:.*migrations.*", "data/"],
"dir_groups": {
"CustomData": ["temp_run/", "scratch/"]
},
"file_groups": {
"Logs": ["*.log"]
}
}
🔒 Safety Rules
By default pypurge REFUSES to run in:
/
$HOME
/usr, /etc, /bin, /sbin
Unless you explicitly pass:
--allow-broad-root
Running as root also requires:
--allow-root
🏗️ Architecture
The project follows a modular structure to separate concerns:
src/pypurge/
├── cli.py # Main entry point, argument parsing
└── modules/
├── backup.py # Atomic backup logic
├── deletion.py # Safe file/directory removal
├── locking.py # Cross-process lock management
├── logging.py # Logging setup
├── safety.py # Guards against dangerous operations
├── scan.py # Core target scanning logic
├── ui.py # Rich terminal output
└── utils.py # Helper functions
The core logic flow is:
- Parse Arguments:
cli.pyhandles user input. - Acquire Lock:
locking.pyprevents concurrent runs in the same directory. - Scan for Targets:
scan.pyidentifies files and directories to be deleted based on predefined and custom patterns. - Confirm & Backup: The user is prompted to confirm, and an optional backup is created using
backup.py. - Delete:
deletion.pyremoves the targets. - Release Lock: The lock is released.
🗺️ Roadmap
Our vision for pypurge is just getting started. We have ambitious plans for new features, integrations, and AI-powered capabilities.
To see the full, detailed plan, please check out our official Project Roadmap.
🤝 Trusted Publishing & CI
This project uses PyPI Trusted Publishing (OIDC) + GitHub Actions for secure releases.
Push tag to publish:
git tag v0.1.0 git push origin v0.1.0
🧠 Requirements
Python >= 3.8
- rich (for beautiful console output)
🪪 License
MIT © Dhruv
⭐ Support the Project
If this tool saved you from rm -rf nightmares… Give it a ⭐ on GitHub — it helps a lot!
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pypurge-3.0.2.tar.gz.
File metadata
- Download URL: pypurge-3.0.2.tar.gz
- Upload date:
- Size: 38.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
768f40b31fa25d25b5f38e97f645f06a61919a0880e5bb4159750b5ac431a37e
|
|
| MD5 |
a9a6eed3a9ce78d04f3b05e7b9d872b8
|
|
| BLAKE2b-256 |
f3ccadf657dd94c16488d88d383df7b34c9cae332b66567c0a6fa716fc468417
|
Provenance
The following attestation bundles were made for pypurge-3.0.2.tar.gz:
Publisher:
publish.yml on dhruv13x/pypurge
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pypurge-3.0.2.tar.gz -
Subject digest:
768f40b31fa25d25b5f38e97f645f06a61919a0880e5bb4159750b5ac431a37e - Sigstore transparency entry: 725337010
- Sigstore integration time:
-
Permalink:
dhruv13x/pypurge@e66802a6bed9b44412e81bb68deb62f489bd74e1 -
Branch / Tag:
refs/tags/v3.0.2 - Owner: https://github.com/dhruv13x
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@e66802a6bed9b44412e81bb68deb62f489bd74e1 -
Trigger Event:
push
-
Statement type:
File details
Details for the file pypurge-3.0.2-py3-none-any.whl.
File metadata
- Download URL: pypurge-3.0.2-py3-none-any.whl
- Upload date:
- Size: 20.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
92a0040f5ba0e09f92edb89c6b181453cba5a3c8bb98fd76151d9358194df889
|
|
| MD5 |
11e7a8e23d5282d0c14a899e1660b788
|
|
| BLAKE2b-256 |
3d27f2d80f4ddd2dccf3173636927a8bce8960d010bc99d7f3fb1b6b25863994
|
Provenance
The following attestation bundles were made for pypurge-3.0.2-py3-none-any.whl:
Publisher:
publish.yml on dhruv13x/pypurge
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pypurge-3.0.2-py3-none-any.whl -
Subject digest:
92a0040f5ba0e09f92edb89c6b181453cba5a3c8bb98fd76151d9358194df889 - Sigstore transparency entry: 725337019
- Sigstore integration time:
-
Permalink:
dhruv13x/pypurge@e66802a6bed9b44412e81bb68deb62f489bd74e1 -
Branch / Tag:
refs/tags/v3.0.2 - Owner: https://github.com/dhruv13x
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@e66802a6bed9b44412e81bb68deb62f489bd74e1 -
Trigger Event:
push
-
Statement type: