Automatically unmonitor seasons in Sonarr when all episodes have been deleted and no future episodes are expected. Prevents unnecessary searches for completed/cancelled seasons.
- Automatic Season Unmonitoring: Unmonitors seasons after episode deletion
- Future Episode Detection: Keeps seasons monitored if future episodes are expected
- Bulk Cleanup Mode: Processes all series at once
- Custom Script Integration: Runs automatically on episode deletion
- Background Execution with Delay: Spawns background process with 10-minute delay to avoid race conditions
- Test Mode: Safe test runs without changes
- Environment Variable Support: Docker-friendly configuration
- Python 3.6+
requestslibrary- Sonarr v3 API
- Configured API connection
- Copy script to Sonarr scripts folder:
cp sonarr-season-unmonitor.py /path/to/sonarr/scripts/
chmod +x /path/to/sonarr/scripts/sonarr-season-unmonitor.py-
Create configuration file (see Configuration)
-
Set up in Sonarr as Custom Script (see Sonarr Setup)
[API]
url = http://localhost:8989
key = your_sonarr_api_key
url_base =# API Configuration
API_KEY=your_sonarr_api_key
SONARR_URL=http://localhost:8989
URL_BASE=/sonarrenvironment:
- API_KEY=your_sonarr_api_key
- SONARR_URL=http://sonarr:8989
- URL_BASE=/sonarr# Runs automatically on episode deletion
# Script spawns a background process with 10-minute delay
# to avoid race conditions with other scripts
/scripts/sonarr-season-unmonitor.pyNote: When triggered by EpisodeFileDelete event, the script immediately spawns a background process with a 10-minute delay and exits. This allows Sonarr to continue processing other tasks while preventing race conditions with other custom scripts that may be running simultaneously.
# Process all series and seasons
/scripts/sonarr-season-unmonitor.py --cleanup-all# Test run without changes
/scripts/sonarr-season-unmonitor.py --test# Test API connection
/scripts/sonarr-season-unmonitor.py --test-config# Adjust delay for background execution (default: 10 minutes)
/scripts/sonarr-season-unmonitor.py --delay-minutes 5| Argument | Description | Default |
|---|---|---|
--test |
Test mode, no changes | False |
--cleanup-all |
Process all series | False |
--test-config |
Test API configuration | False |
--config PATH |
Path to configuration file | Auto-Detection |
--delay-minutes N |
Delay in minutes for background execution | 10 |
--delayed-execution |
Internal flag for background process | Auto-set |
--debug |
Enable debug mode with detailed logging | False |
- Settings → Connect → + → Custom Script
- Name:
Season Unmonitor - On Episode File Delete: ✅ Enabled
- Path:
/scripts/sonarr-season-unmonitor.py - Arguments: (leave empty)
- Click Test in Sonarr
- Check logs:
✅ Successfully connected to Sonarr API
✅ Configuration test completed successfully!
- Episode Deletion Trigger: Script runs when episode is deleted
- Background Process Spawn: Immediately spawns detached background process with configurable delay (default: 10 minutes)
- Main Process Exit: Main process exits immediately, allowing Sonarr to continue
- Delay Wait: Background process waits for specified delay to avoid race conditions
- Season Analysis: Checks if season has any remaining episodes
- Future Episode Check: Verifies no monitored episodes with future air dates
- Unmonitor Decision: Unmonitors season if empty and no future episodes
- Logging: Records all actions for review
When triggered by Sonarr's EpisodeFileDelete event:
- The script immediately spawns a background process and exits
- Sonarr continues processing without waiting
- The background process waits 10 minutes (configurable) before executing
- This prevents race conditions with other scripts that may run on the same event
- Environment variables from Sonarr are automatically inherited by the background process
Season 3 - 10 episodes total
- Episodes 1-8: Deleted
- Episodes 9-10: Never downloaded, air date in past
- Result: Season 3 unmonitored ✅
Season 4 - 12 episodes total
- Episodes 1-8: Deleted
- Episodes 9-10: Never downloaded, air date in future
- Result: Season 4 remains monitored ⏳
The script checks for monitored episodes with:
airDatein the future (after today)monitored = truehasFile = false
This prevents unmonitoring seasons that still have upcoming episodes.
- Path:
/config/logs/sonarr-season-unmonitor.txt - Rotation: Weekly, 4 backups
- Format:
YYYY-MM-DD HH:MM:SS LEVEL Message
- INFO: Normal operations
- DEBUG: Detailed information (
--test) - WARNING: Non-critical issues
- ERROR: Critical errors
2026-01-12 14:30:00 INFO EpisodeFileDelete event detected. Spawning background process with delay...
2026-01-12 14:30:00 INFO Spawning background process with 10 minute delay...
2026-01-12 14:30:00 INFO Background process spawned with PID 12345
2026-01-12 14:30:00 INFO Background process spawned. Main script exiting to allow Sonarr to continue.
... 10 minutes later in background process ...
2026-01-12 14:40:00 INFO Background process started. Waiting 10 minutes (600 seconds) before processing...
2026-01-12 14:40:00 INFO Delay completed. Continuing with execution...
2026-01-12 14:40:01 INFO Processing EpisodeFileDelete event for series 'Breaking Bad' (ID: 1), Season 1
2026-01-12 14:40:02 INFO Season 1 of 'Breaking Bad' has 13 total episodes, 0 still have files
2026-01-12 14:40:03 INFO No episodes with files remaining in season 1 and no future monitored episodes. Unmonitoring season.
2026-01-12 14:40:04 INFO Successfully unmonitored season 1 for series 'Breaking Bad'
Environment variable not found
❌ sonarr_eventtype not found in environment variables- Run in
--cleanup-allmode for manual execution - Check Custom Script configuration in Sonarr
API connection failed
❌ Failed to connect to Sonarr API- Check Sonarr URL
- Validate API key
- Test network connection
No configuration found
❌ No configuration file found and no environment variables set- Create config file
- Set environment variables
# Test configuration
/scripts/sonarr-season-unmonitor.py --test-config
# Test mode with full output
/scripts/sonarr-season-unmonitor.py --test --cleanup-all- Read-Only Tests: Test mode only reads data
- Conservative Logic: Only unmonitors when certain
- Validation: Checks all inputs before processing
- Sonarr Database: Regular backups
- Config Files: Version control configuration
- Monitor Settings: Note current monitoring state
- Environment Variables (highest priority)
- Config File
- Default Values (lowest priority)
- Efficient API Usage: Minimal requests
- Smart Processing: Only checks relevant seasons
- Robust Error Handling: Continues on errors
- Background Execution: Doesn't block Sonarr operations
- Single Season: ~1-2 seconds (after 10-minute delay)
- Series (10 seasons): ~5-10 seconds
- Bulk Mode (100 series): ~2-5 minutes
# Triggered by Sonarr when episode is deleted
# Environment variables automatically set:
# sonarr_eventtype=EpisodeFileDelete
# sonarr_series_id=123
# sonarr_series_title=Breaking Bad
# sonarr_episodefile_seasonnumber=1
#
# Script immediately spawns background process and exits
# Background process waits 10 minutes before processing# Clean up all series manually
/scripts/sonarr-season-unmonitor.py --cleanup-allservices:
sonarr:
environment:
- API_KEY=${SONARR_API_KEY}
- SONARR_URL=http://sonarr:8989
volumes:
- ./scripts:/scripts# Weekly cleanup at Sunday 2 AM
0 2 * * 0 /scripts/sonarr-season-unmonitor.py --cleanup-all/scripts/sonarr-season-unmonitor.py --test-configExpected Output:
✅ Successfully connected to Sonarr API
✅ System status: OK
✅ Configuration test completed successfully!
/scripts/sonarr-season-unmonitor.py --test --cleanup-allExpected Output:
🧪 TEST MODE: No changes will be made
📺 Processing series: Breaking Bad (ID: 1)
📅 Season 1: Would unmonitor (no files, no future episodes)
📅 Season 2: Keeping monitored (has future episodes)
The script uses a conservative approach:
- Only unmonitors when absolutely certain
- Preserves monitoring for edge cases
- Logs all decisions for transparency
A season is kept monitored if it has any monitored episode with:
airDate> todaymonitored = truehasFile = false
This handles:
- Announced but not yet aired episodes
- Episodes with uncertain air dates
- Upcoming season continuations