A Python-based Docker solution to clean up orphaned directories that qBitTorrent fails to delete when using auto-unpackers.
When using qBitTorrent with auto-unpacking features, it sometimes fails to delete the unpacked files along with their folders when the torrent is deleted. This script monitors your qBitTorrent instance and cleans up these leftover directories.
- Works with qBitTorrent 4.5+
- Compatible with qBitTorrent 5.x
- Uses the qBitTorrent WebUI API (v2025.5.0)
Note regarding qBitTorrent 5.x: While this issue was common in qBitTorrent 4.x versions, it appears to still exist in some scenarios in qBitTorrent 5.x, particularly when using external tools for auto-extraction. A recent GitHub issue (#21550) confirms this problem is still present in version 5.0.0.
- Docker
- Docker Compose
- qBitTorrent with WebUI enabled
git clone https://github.com/Jarsky/qBitTorrent-Cleanup-Script
cd qBitTorrent-Cleanup-ScriptCopy the example environment file and edit it with your settings:
cp env.example.env .envEdit the .env file with your qBitTorrent settings:
# qBitTorrent WebUI settings
QB_HOST=192.168.1.100
QB_PORT=8080
QB_USERNAME=admin
QB_PASSWORD=yourpassword
# Paths
QB_DOWNLOAD_PATH=/downloads
LOG_FILE=/logs/qbt-cleanup.log
# Script behavior
DRY_RUN=false
# Execution mode: "daemon" or "cron"
EXECUTION_MODE=daemon
# Cron schedule (when EXECUTION_MODE=cron)
CRON_SCHEDULE="0 */4 * * *"
Edit the docker-compose.yml file to map your actual qBitTorrent downloads directory:
version: '3'
services:
qbt-cleanup:
build: .
container_name: qbt-cleanup
volumes:
- ./logs:/logs
- /actual/path/to/qbittorrent/downloads:/downloads # Update this path
env_file:
- .env
restart: unless-stoppedMake sure to replace /actual/path/to/qbittorrent/downloads with the actual path on your host where qBitTorrent stores downloaded files. This path must match the download path configured in your qBitTorrent settings.
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 your qBitTorrent download directory |
| LOG_FILE | /logs/qbt-cleanup.log | Path to log file |
| DRY_RUN | false | Set to true to test without making changes |
| EXECUTION_MODE | daemon | Run mode: "daemon" (continuous) or "cron" (scheduled) |
| SLEEP_INTERVAL | 14400 | Time in seconds between runs when in daemon mode |
| CRON_SCHEDULE | "0 */4 * * *" | Cron expression for scheduling when in cron mode |
In daemon mode, the script runs continuously, checking for orphaned directories at regular intervals defined by SLEEP_INTERVAL. This is useful when you want to maintain a lightweight, continuously running process.
In cron mode, the script runs according to the schedule defined in CRON_SCHEDULE. This is useful when you want precise control over when cleanups occur and prefer a scheduled task approach.
The script produces detailed logs similar to other torrent management tools, including:
- Script version information
- Connection status and login results
- qBitTorrent version and API version
- Client status (download/upload speeds and totals)
- Number of torrents found
- Details about orphaned directories
- Actions taken (deletions) or simulated actions (in dry run mode)
- Comprehensive error messages
Logs are written both to the console and to the file specified by LOG_FILE.
Example log output:
Sat, 01 Jun 2024 10:00:01 qbt-cleanup INFO: qBitTorrent Cleanup 1.0.0
Sat, 01 Jun 2024 10:00:01 qbt-cleanup INFO: Starting cleanup process...
Sat, 01 Jun 2024 10:00:01 qbt-cleanup INFO: Connecting to qBitTorrent at 192.168.1.100:8080...
Sat, 01 Jun 2024 10:00:01 qbt-cleanup INFO: Logging in...
Sat, 01 Jun 2024 10:00:01 qbt-cleanup INFO: Login successful. Client is qBitTorrent v5.0.0
Sat, 01 Jun 2024 10:00:01 qbt-cleanup INFO: WebUI API version: 2.8.18
Sat, 01 Jun 2024 10:00:01 qbt-cleanup INFO: Status reported by the client:
Sat, 01 Jun 2024 10:00:01 qbt-cleanup INFO: Download Speed: 0.00B/s Total: 1.25TiB
Sat, 01 Jun 2024 10:00:01 qbt-cleanup INFO: Upload Speed: 2.45MiB/s Total: 10.88TiB
Sat, 01 Jun 2024 10:00:01 qbt-cleanup INFO: Getting all the torrents...
Sat, 01 Jun 2024 10:00:02 qbt-cleanup INFO: Found 157 torrent(s) in the client.
Sat, 01 Jun 2024 10:00:02 qbt-cleanup INFO: Identified 157 unique torrent names
Sat, 01 Jun 2024 10:00:02 qbt-cleanup INFO: Checking download path: /downloads
Sat, 01 Jun 2024 10:00:02 qbt-cleanup INFO: Found 3 orphaned directories
Sat, 01 Jun 2024 10:00:02 qbt-cleanup INFO: Deleting orphaned directory: /downloads/orphaned-folder-1
Sat, 01 Jun 2024 10:00:02 qbt-cleanup INFO: Successfully deleted: /downloads/orphaned-folder-1
If you prefer to run without Docker:
- Install Python 3.7+
- Install requirements:
pip install -r requirements.txt - Run the script:
python qBitTorrent-Cleanup.py
--dry-run: Test mode - doesn't actually delete files--daemon: Run continuously at specified intervals--interval 3600: Time in seconds between runs when in daemon mode--run-once: Run once and exit (for cron scheduling)
The script:
- Connects to qBitTorrent's WebUI API
- Gets a list of all current torrents
- Scans the download directory for folders
- Removes any folders that don't correspond to current torrents
- Logs all actions
- No dependencies on shell utilities or qBitTorrent log files
- Cross-platform compatibility
- Direct API access instead of log parsing
- Better error handling
- Easy configuration via environment variables
- Containerized for easy deployment
- Flexible scheduling options (daemon or cron)