# Plex Cleanup Analyzer A tool to identify movies and TV shows in your Plex library that are candidates for deletion based on ratings, watch history, and age. Integrates with Radarr and Sonarr to properly unmonitor content before deletion, preventing automatic re-downloads while keeping database entries for future requests via Ombi. ## Overview This tool helps you reclaim storage by finding content that: - Has poor ratings (below your threshold) - Has never been watched by anyone - Has been in your library for a long time It generates an interactive selection list that you can review and edit before any deletions occur. ## Requirements - Python 3.8+ - Access to your Plex database file - Radarr and Sonarr API access (for cleanup execution) ### Python Dependencies All dependencies are from the standard library: - `sqlite3`, `dataclasses`, `urllib`, `json`, `os`, `pathlib` Optional for `.env` support: ```bash pip install python-dotenv ``` ## Configuration ### Environment Variables (.env file) Create a `.env` file in the same directory as the script: ```env RADARR_URL=https://radarr.example.com RADARR_API_KEY=your_radarr_api_key_here SONARR_URL=https://sonarr.example.com SONARR_API_KEY=your_sonarr_api_key_here ``` ### Finding Your API Keys **Radarr:** 1. Go to Settings → General 2. Copy the "API Key" value **Sonarr:** 1. Go to Settings → General 2. Copy the "API Key" value ## Usage ### Step 1: Test Connections First, verify your API credentials are working: ```bash python3 plex_cleanup_analyzer.py --test-connections ``` Expected output: ``` Testing connections... plex: ✓ Connected - found 5 libraries radarr: ✓ Connected - 1234 movies in database sonarr: ✓ Connected - 567 series in database ``` ### Step 2: Generate Selection List Generate a list of deletion candidates: ```bash python3 plex_cleanup_analyzer.py \ --selection-list candidates.txt \ --never-watched \ --min-days 730 \ --libraries 2,3,5 \ --exclude-genres "Sport,Documentary" ``` **Options explained:** - `--selection-list FILE` - Output file for the interactive list - `--never-watched` - Only include items with zero plays - `--min-days N` - Only include items added at least N days ago (730 = 2 years) - `--max-rating N` - Only include items rated below N (default: 5.0) - `--libraries IDS` - Comma-separated library IDs to scan - `--exclude-genres LIST` - Genres to mark as protected (won't be selected) ### Step 3: Find Your Library IDs If you don't know your library IDs: ```bash python3 plex_cleanup_analyzer.py --list-libraries ``` Output example: ``` Libraries in Plex: ID: 1 - Photos (photo) ID: 2 - Movies (movie) ID: 3 - TV Shows (show) ID: 4 - Music (artist) ID: 5 - Anime (show) ``` ### Step 4: Review and Select Items Open `candidates.txt` in any text editor. The format is: ``` # MOVIES (4474 items, 11907.23 GB total) # Rating | Size | Added | Title [ ] 2.1 | 1.42 GB | 2021-03-15 | Bad Movie (2020) [ ] 3.5 | 2.81 GB | 2020-11-22 | Mediocre Film (2019) [P] 4.2 | 8.50 GB | 2022-01-10 | Documentary About Nature (2021) # TV SHOWS (801 items, 25680.78 GB total) # Rating | Size | Added | Title [ ] 2.8 | 45.20 GB | 2019-05-03 | Cancelled Show (2018) [P] 6.5 | 12.30 GB | 2020-08-17 | Sports Documentary (2019) ``` **Selection markers:** - `[ ]` - Not selected (will be kept) - `[X]` - Selected for deletion (change from `[ ]` to `[X]`) - `[P]` - Protected (cannot be deleted - matches genre/title protection rules) **To select items for deletion:** 1. Change `[ ]` to `[X]` for items you want to delete 2. Save the file Example after editing: ``` [X] 2.1 | 1.42 GB | 2021-03-15 | Bad Movie (2020) [ ] 3.5 | 2.81 GB | 2020-11-22 | Mediocre Film (2019) ``` ### Step 5: Preview Changes (Dry Run) Before executing, preview what will happen: ```bash python3 plex_cleanup_analyzer.py --process-selection candidates.txt --dry-run ``` This shows what would be deleted without making any changes. ### Step 6: Execute Deletion When you're ready to delete: ```bash python3 plex_cleanup_analyzer.py --process-selection candidates.txt ``` **What happens during execution:** 1. For each selected movie: - Finds the movie in Radarr by TMDB ID - Unmonitors the movie (prevents re-download) - Deletes the movie files - Keeps the Radarr database entry (for Ombi re-requests) 2. For each selected TV show: - Finds the series in Sonarr by TVDB ID - Unmonitors the series (prevents re-download) - Deletes all episode files - Keeps the Sonarr database entry (for Ombi re-requests) ## Protection System ### Genre Protection Content in protected genres is marked `[P]` and cannot be selected: - Sport - Documentary (optional) ### Title Pattern Protection Content matching these patterns is also protected: - 3D movies (titles containing "3D") - Surfing content - Skiing/snowboarding content - Extreme sports ### Custom Protection Edit the `cleanup_config.yaml` file to customize: ```yaml protected_genres: - Sport - Documentary - Concert protected_title_patterns: - "3D" - "IMAX" - "surf" - "ski" excluded_libraries: - 1 # Photos - 4 # Music ``` ## Command Reference | Command | Description | |---------|-------------| | `--test-connections` | Test Plex, Radarr, and Sonarr connections | | `--list-libraries` | Show all Plex libraries with IDs | | `--selection-list FILE` | Generate interactive selection list | | `--process-selection FILE` | Process selections from file | | `--dry-run` | Preview changes without executing | | `--never-watched` | Only include unwatched items | | `--min-days N` | Minimum age in days | | `--max-rating N` | Maximum rating threshold | | `--libraries IDS` | Library IDs to include | | `--exclude-genres LIST` | Genres to protect | ## How It Works ### Database Analysis The tool queries your Plex SQLite database directly to find: - `metadata_items` - Movie and show information - `metadata_item_settings` - View counts per user - `media_parts` - File sizes and paths - `tags` / `taggings` - Genre information - `guid` - External IDs (TMDB, TVDB) ### Rating Logic - Uses `audience_rating` for TV shows (critic ratings are often empty) - Uses `rating` for movies when available - Falls back to audience_rating if critic rating is missing ### *arr Integration The tool matches Plex content to Radarr/Sonarr using: - **Movies:** TMDB ID from Plex GUID → Radarr's tmdbId - **TV Shows:** TVDB ID from Plex GUID → Sonarr's tvdbId ## Troubleshooting ### "No movies found in Radarr matching TMDB ID" The movie exists in Plex but not in Radarr. It may have been: - Added manually to Plex - Deleted from Radarr already - Have mismatched metadata ### "Connection failed" Check your `.env` file: - Verify URLs include `https://` or `http://` - Verify API keys are correct - Ensure services are accessible from this machine ### "No candidates found" Your criteria may be too strict. Try: - Increasing `--max-rating` (e.g., 6.0) - Decreasing `--min-days` (e.g., 365) - Removing `--never-watched` ## Safety Features 1. **Selection file review** - You manually choose what to delete 2. **Protected content** - Important items marked `[P]` can't be selected 3. **Dry run mode** - Preview before executing 4. **Database preservation** - *arr entries kept for re-requests 5. **Unmonitor first** - Prevents automatic re-downloads ## File Locations | File | Purpose | |------|---------| | `plex_cleanup_analyzer.py` | Main script | | `.env` | API credentials | | `cleanup_config.yaml` | Optional advanced config | | `candidates.txt` | Generated selection list |