This repository now has one active implementation and two Bash scripts kept only for legacy compatibility.
-
Python/qBitTorrent-Cleanup.py- The primary and recommended implementation.
- Uses the qBitTorrent WebUI API through
qbittorrent-api. - Intended for qBitTorrent 4.5+ and compatible with qBitTorrent 5.x.
-
Bash/4.5+/qBitTorrent-Cleanup.sh- Legacy Bash script for qBitTorrent 4.5 and newer.
- Kept for reference or older existing setups.
-
Bash/pre-4.5/qBitTorrent-Cleanup.sh- Legacy Bash script for qBitTorrent versions before 4.5.
- Kept only because older qBitTorrent log formats differ.
Use the Python version unless you specifically need one of the legacy Bash scripts.
The Bash scripts are archived for historical compatibility. They still rely on hard-coded paths and older assumptions, so they should be treated as legacy tools rather than the preferred deployment path.
When qBitTorrent is used with auto-unpacking tools, it can leave behind extracted folders after a torrent is removed. This repository provides cleanup scripts to remove those orphaned directories.
The Python version is the main implementation and the one the Docker setup runs.
- Direct qBitTorrent WebUI API access
- Docker-friendly deployment
- Dry-run support
- Daemon or cron-style execution
- Safer directory matching based on qBitTorrent-reported paths
- Guardrails for excluded directory names, minimum directory age, and empty-client protection
- Docker
- Docker Compose
- qBitTorrent with WebUI enabled
- Clone the repository:
git clone https://github.com/Jarsky/qBitTorrent-Cleanup-Script
cd qBitTorrent-Cleanup-Script- Create your environment file:
cp env.example.env .env-
Update
.envwith your qBitTorrent settings. -
Update
docker-compose.ymlso your real download directory is mounted to/downloads. -
Start the container:
docker-compose up -dThe following environment variables can be configured in .env:
| Variable | Default | Description |
|---|---|---|
| QB_HOST | localhost | qBitTorrent WebUI host |
| QB_PORT | 8080 | qBitTorrent WebUI port |
| QB_USERNAME | admin | qBitTorrent WebUI username |
| QB_PASSWORD | adminadmin | qBitTorrent WebUI password |
| QB_DOWNLOAD_PATH | /downloads | Path to the qBitTorrent download directory inside the runtime |
| LOG_FILE | /logs/qbt-cleanup.log | Path to log file |
| DRY_RUN | false | Set to true to test without making changes |
| EXCLUDED_DIR_NAMES | .stfolder,@eaDir,lost+found | Comma-separated top-level directories that should never be deleted |
| MIN_DIR_AGE_SECONDS | 0 | Skip deleting directories newer than this many seconds |
| ALLOW_EMPTY_CLIENT | false | If false, cleanup is skipped when qBitTorrent reports zero torrents |
| EXECUTION_MODE | daemon | Run mode: daemon or cron |
| SLEEP_INTERVAL | 14400 | Time in seconds between runs when in daemon mode |
| CRON_SCHEDULE | 0 */4 * * * |
Cron expression used when EXECUTION_MODE=cron |
- Connects to the qBitTorrent WebUI API.
- Retrieves the current torrent list.
- Protects active top-level download directories using qBitTorrent-reported paths.
- Scans the configured download root for top-level directories.
- Deletes directories that are no longer in use, while respecting the configured safety guardrails.
- The Python cleanup logic only evaluates top-level directories inside
QB_DOWNLOAD_PATH. - A dedicated per-torrent folder layout is strongly recommended.
- Cleanup is skipped by default if qBitTorrent reports zero torrents.
- Use
DRY_RUN=truefirst whenever you change download paths or exclusions.
If you want to run the Python version directly:
- Install Python 3.7+.
- Install dependencies:
pip install -r requirements.txt- Run the script:
python Python/qBitTorrent-Cleanup.py--dry-run--daemon--interval 3600--run-once
The Bash scripts are kept for legacy purposes only.
- For qBitTorrent versions before 4.5.
- Relies on the older log message format such as
Error: Directory not empty.
- For qBitTorrent 4.5 and newer.
- Includes both
-legacylog parsing and-runmode viaqbittorrent-cli.
- The Bash scripts are not the recommended path for new users.
- They use hard-coded paths and older operational assumptions.
- If you still use them, run them in test mode first.
- No dependency on qBitTorrent log parsing for normal operation
- Safer matching against active torrent paths
- Better error handling
- Easier Docker deployment
- Clearer configuration via environment variables