Automatically delete old episodes in Sonarr to save disk space. The script keeps a configurable number of the newest episodes per series and automatically deletes older ones.
- Automatic Episode Retention: Deletes old episodes based on configurable limits
- Cross-Season Support: Episode limits work across multiple seasons
- Smart Unmonitoring: Unmonitors old episodes and after deletion
- Flexible Configuration: Config file or environment variables
- Custom Script Integration: Runs automatically on new downloads
- Debug Mode: Safe test runs without changes
- API Testing: Configuration validation
- Python 3.6+
requestslibrary- Sonarr v3 API
- Configured API connection
- Copy script to Sonarr scripts folder:
cp sonarr-episode-retention.py /path/to/sonarr/scripts/
chmod +x /path/to/sonarr/scripts/sonarr-episode-retention.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 =
[Series]
breaking-bad = 10
the-office-us = 15
game-of-thrones = 5# API Configuration
API_KEY=your_sonarr_api_key
SONARR_URL=http://localhost:8989
URL_BASE=/sonarr
# Series Configuration
SERIES_breaking-bad=10
SERIES_the-office-us=15
SERIES_game-of-thrones=5environment:
- API_KEY=your_sonarr_api_key
- SONARR_URL=http://sonarr:8989
- SERIES_breaking-bad=10
- SERIES_the-office-us=15# Runs automatically on downloads
/scripts/sonarr-episode-retention.py# Clean all configured series
/scripts/sonarr-episode-retention.py --custom-script=false# Test run without changes
/scripts/sonarr-episode-retention.py --debug# Display available series with cleanTitle
/scripts/sonarr-episode-retention.py --list-series| Argument | Description | Default |
|---|---|---|
--debug |
Debug mode, no changes | False |
--config PATH |
Path to configuration file | Auto-Detection |
--list-series |
Show available series | False |
--custom-script |
Custom Script mode | True |
- Settings β Connect β + β Custom Script
- Name:
Episode Retention - On File Import: β Enabled
- On File Import: β Enabled
- Path:
/scripts/sonarr-episode-retention.py - Arguments: (leave empty)
- Click Test in Sonarr
- Check logs:
β
Successfully connected to Sonarr API
β
Found 3 series in configuration
β
Configuration test completed successfully!
- Episode Collection: Gets all episodes with files
- Chronological Sorting: Sorted by season and episode
- Limit Application: Keeps the last X episodes
- Old Episode Unmonitoring: Unmonitors episodes before first download
- Delete + Unmonitor: Deletes old files and unmonitors episodes
Series with 30 episodes, limit: 15
- Keep: Episodes 16-30 (newest 15)
- Delete: Episodes 1-15 (oldest)
- Unmonitor: All episodes before episode 16
- Path:
/config/logs/sonarr-episode-retention.txt - Rotation: Weekly, 4 backups
- Format:
YYYY-MM-DD HH:MM:SS LEVEL Message
- INFO: Normal operations
- DEBUG: Detailed information (
--debug) - WARNING: Non-critical issues
- ERROR: Critical errors
2025-09-11 14:30:00 INFO Processing: Breaking Bad
2025-09-11 14:30:01 INFO Series has 50 downloaded episodes, keeping 10, deleting 40
2025-09-11 14:30:02 INFO Unmonitored 5 old episodes
2025-09-11 14:30:10 INFO Successfully processed 40 episodes for deletion
API connection failed
β Failed to connect to Sonarr API- Check Sonarr URL
- Validate API key
- Test network connection
Series not found
series 'wrong-name' from config not found in sonarr- Run
--list-seriesfor available names - Use
cleanTitleinstead oftitle
No series configuration
β οΈ No series configuration found- Check config file
- Validate environment variables
# Full debug output
/scripts/sonarr-episode-retention.py --debug --list-series
# Test mode
/scripts/sonarr-episode-retention.py --debug- Test Events: Only GET requests, no changes
- Debug Mode: Simulates actions without execution
- Validation: Checks all inputs before processing
- Sonarr Database: Regular backups
- Media Files: Backup before first use
- Config Files: Version control configuration
- Environment Variables (highest priority)
- Config File
- Default Values (lowest priority)
- Batch Operations: Efficient API usage
- Smart Filtering: Only process relevant episodes
- Robust Error Handling: Skips problematic entries
- 10 episodes: ~2-5 seconds
- 100 episodes: ~10-30 seconds
- 1000 episodes: ~1-3 minutes
# Via config file
echo "new-series = 12" >> /scripts/retention.conf
# Via environment variable
export SERIES_new-series=12services:
sonarr:
environment:
- API_KEY=${SONARR_API_KEY}
- SONARR_URL=http://sonarr:8989
- SERIES_breaking-bad=10
- SERIES_the-office-us=15# Daily at 3 AM
0 3 * * * /scripts/sonarr-episode-retention.py --custom-script=false