Complete Guide: Automatic Tracker Removal for qBittorrent

This guide will help you set up a Python script that automatically removes unwanted trackers from your qBittorrent downloads. The script will run as a Windows service, monitoring for new downloads continuously, even when you’re not logged in.

Prerequisites

  • Windows operating system
  • qBittorrent installed with Web UI enabled
  • Python installed (3.6 or later recommended)
  • NSSM (Non-Sucking Service Manager) - for running the script as a Windows service

Step 1: Enable qBittorrent Web UI

  1. Open qBittorrent
  2. Go to Tools > Options > Web UI
  3. Check “Web User Interface (Remote Control)”
  4. Set a username and password
  5. Make note of the IP address (default: 127.0.0.1) and port (default: 8080)
  6. Click “Apply” and “OK”

Step 2: Install Required Python Library

  1. Open Command Prompt (search “cmd” in Windows start menu)

  2. Install the qBittorrent API library:

    pip install qbittorrent-api
    

Step 3: Create the Folder Structure

  1. Create a folder for your automation scripts:

    C:\Users\markk\Automation\Torrent
    

Step 4: Create the Python Script

  1. Create a new file named remove_tracker.py in your automation folder
  2. Copy and paste the following code:
import qbittorrentapi
import time
import logging
import os
import sys
from datetime import datetime
 
# Set up logging in your specified directory
log_dir = r"C:\Users\markk\Automation\Torrent"
os.makedirs(log_dir, exist_ok=True)
log_file = os.path.join(log_dir, 'tracker_remover.log')
 
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s',
    handlers=[
        logging.FileHandler(log_file)
        # No console output when running as a service
    ]
)
 
# CONFIGURATION
QB_HOST = "localhost"
QB_PORT = 8080
QB_USERNAME = "your_username"  # Replace with your username
QB_PASSWORD = "your_password"  # Replace with your password
 
# The specific tracker to remove
UNWANTED_TRACKER = "tracker.p2p-world.net"
 
# How often to check (in seconds)
CHECK_INTERVAL = 30
 
def main():
    # Connect to qBittorrent
    logging.info(f"Starting qBittorrent tracker remover service")
    
    # Give qBittorrent time to start if computer just booted
    time.sleep(30)
    
    logging.info(f"Connecting to qBittorrent at {QB_HOST}:{QB_PORT}")
    
    while True:
        try:
            qbt_client = qbittorrentapi.Client(
                host=QB_HOST,
                port=QB_PORT,
                username=QB_USERNAME,
                password=QB_PASSWORD
            )
            
            # Login
            qbt_client.auth_log_in()
            logging.info("Successfully connected to qBittorrent")
            break
        except Exception as e:
            logging.error(f"Connection error: {e}")
            logging.info("Will retry in 60 seconds...")
            time.sleep(60)
            continue
 
    # Track which torrents we've already processed
    processed_torrents = set()
 
    # Main loop
    try:
        while True:
            try:
                # Get only active torrents
                torrents = qbt_client.torrents_info(filter='downloading')
                logging.info(f"Checking {len(torrents)} active downloads")
                
                for torrent in torrents:
                    # Skip already processed torrents
                    if torrent.hash in processed_torrents:
                        continue
                        
                    logging.info(f"Processing: {torrent.name}")
                    
                    # Get trackers for this torrent
                    trackers = qbt_client.torrents_trackers(torrent.hash)
                    
                    # Find trackers to remove
                    tracker_urls_to_remove = []
                    for tracker in trackers:
                        if UNWANTED_TRACKER in tracker.url:
                            tracker_urls_to_remove.append(tracker.url)
                    
                    # Remove the unwanted trackers
                    if tracker_urls_to_remove:
                        logging.info(f"Removing {len(tracker_urls_to_remove)} trackers from {torrent.name}")
                        for url in tracker_urls_to_remove:
                            logging.info(f"  - Removing: {url}")
                        qbt_client.torrents_remove_trackers(torrent.hash, tracker_urls_to_remove)
                    else:
                        logging.info(f"No matching trackers found in {torrent.name}")
                    
                    # Mark this torrent as processed
                    processed_torrents.add(torrent.hash)
            
            except Exception as e:
                logging.error(f"Error during processing: {e}")
                # Try to reconnect if needed
                try:
                    qbt_client.auth_log_in()
                except:
                    pass
                    
            # Wait before checking again
            time.sleep(CHECK_INTERVAL)
                
    except KeyboardInterrupt:
        logging.info("Service stopped")
        try:
            qbt_client.auth_log_out()
        except:
            pass
 
if __name__ == "__main__":
    main()
  1. Update the following variables in the script:
    • QB_USERNAME and QB_PASSWORD - Set to your qBittorrent Web UI credentials
    • UNWANTED_TRACKER - Set to the tracker you want to remove (“tracker.p2p-world.net” is already set)
    • CHECK_INTERVAL - Adjust if needed (default is 30 seconds)

Step 5: Create a Batch File as Intermediate

  1. Create a new text file named run_tracker.bat in the same folder
  2. Add the following content (adjust the Python path if needed):
@echo off
"C:\Python310\python.exe" "C:\Users\markk\Automation\Torrent\remove_tracker.py"

Step 6: Download and Install NSSM

  1. Download NSSM (Non-Sucking Service Manager) from http://nssm.cc/
  2. Extract the ZIP file to a folder on your computer
  3. For 64-bit Windows, use the executable in the win64 folder

Step 7: Set Up the Windows Service with NSSM

  1. Open Command Prompt as Administrator

  2. Navigate to the directory where you extracted NSSM

    cd path\to\nssm\win64
    
  3. Run NSSM to install the service:

    nssm install QBittorrentTrackerRemover
    
  4. In the NSSM application form that appears:

    Application tab:

    • Path: Enter the full path to your batch file (e.g., C:\Users\markk\Automation\Torrent\run_tracker.bat)
    • Startup directory: C:\Users\markk\Automation\Torrent
    • Arguments: Leave empty (the batch file contains the arguments)

    Details tab:

    • Display name: QBittorrent Tracker Remover
    • Description: Automatically removes unwanted trackers from qBittorrent downloads
    • Startup type: Automatic

    Log on tab:

    • Choose “Log on as” and select “This account”
    • Enter your Windows username and password (this allows the service to interact with qBittorrent running in your user session)

    I/O tab:

    • Output (stdout): C:\Users\markk\Automation\Torrent\service_out.log
    • Error (stderr): C:\Users\markk\Automation\Torrent\service_error.log
  5. Click “Install service”

Step 8: Start and Test the Service

  1. Open Services (press Win+R, type services.msc, and press Enter)
  2. Find “QBittorrent Tracker Remover” in the list
  3. Right-click and select “Start”
  4. Check the service status to ensure it’s “Running”

Step 9: Test and Verify

  1. Open qBittorrent and start downloading a torrent
  2. Check the log file at C:\Users\markk\Automation\Torrent\tracker_remover.log
  3. After a few minutes, you should see entries showing that the script is checking for and removing unwanted trackers

Managing the Service

  • To stop the service: Open Services, find “QBittorrent Tracker Remover”, right-click and select “Stop”

  • To restart the service: Right-click and select “Restart”

  • To uninstall the service: Open an Administrator Command Prompt, navigate to the NSSM directory, and run:

    nssm remove QBittorrentTrackerRemover
    
  • To update the service configuration: Use:

    nssm edit QBittorrentTrackerRemover
    

Troubleshooting

Service doesn’t start

  • Check the service status in Services (services.msc)
  • Look at the service logs: service_out.log and service_error.log
  • Check Windows Event Viewer for errors related to the service

Service starts but doesn’t connect to qBittorrent

  • Make sure qBittorrent is running
  • Verify Web UI is enabled with the correct credentials
  • Check if the port is correct (usually 8080)
  • Make sure the service is running as a user that has access to qBittorrent

Intermediate Batch File Issues

  • Make sure the paths in the batch file are correct
  • Use full absolute paths with proper quoting
  • Verify the batch file works when run directly (double-click it)

Cannot install or configure NSSM

  • Make sure you’re running Command Prompt as Administrator
  • Use the correct version of NSSM (win32 or win64) for your system

Customization

Adding multiple trackers

To remove multiple trackers, modify the UNWANTED_TRACKER section of the script:

# Multiple trackers to remove
UNWANTED_TRACKERS = [
    "tracker.p2p-world.net",
    "another-tracker.com",
    "bad-tracker.org"
]
 
# Then change the tracker removal check to:
for tracker in trackers:
    for unwanted in UNWANTED_TRACKERS:
        if unwanted in tracker.url:
            tracker_urls_to_remove.append(tracker.url)
            break

Changing the check frequency

Adjust the CHECK_INTERVAL value (in seconds) to check more or less frequently.

Why This Approach Is Better Than Other Methods

  • Runs as a proper Windows service - continues running even when not logged in
  • Automatic restart - if the script crashes, Windows will restart it
  • System integration - proper service management through Windows Services
  • Startup dependency - can be configured to start after qBittorrent or other dependencies
  • Better reliability - Windows monitors and manages the process

Final Notes

  • The service will run whether you’re logged in or not
  • It only removes the specified tracker(s) from new downloads
  • The script won’t affect already processed torrents, even after a restart
  • Log files will grow over time, so consider cleaning them occasionally
  • If you update the script, you’ll need to restart the service for changes to take effect