#!/bin/bash # ============================================================================================== # ================================= Sonarr Cleanup ============================================= # ============================================================================================== # # PURPOSE # ───────────────────────────────────────────────────────────────────────────── # Delete orphaned TV episode files not tracked by Sonarr. Queries the API for # all tracked episode file paths, walks the library on disk, and removes anything # untracked that is old enough to be past the import window. Triggers an Emby # library clean after each deletion run so ghost entries disappear immediately. # # ============================================================================================== # OPERATIONAL MODEL # ============================================================================================== # # Every file encountered on disk is classified into one of five categories: # # TRACKED — Sonarr API knows this exact path → leave it alone # PROTECTED — matches SONARR_PROTECTED_PATTERNS → never delete # ORPHAN — video file, not tracked, older than SONARR_ORPHAN_AGE → delete # JUNK — not a video extension, not protected → delete regardless of age # RECENT — not tracked, under SONARR_ORPHAN_AGE → skip (may be mid-import) # # Sonarr generates show artwork (*.jpg), metadata (*.nfo), and manages subtitles # (*.srt, *.sub, *.ass) but does NOT include these in its tracked file API response. # Without PROTECTED classification these would be deleted — breaking Sonarr and # Emby metadata display. # # After deletions: notify_emby_scan() triggers Emby "Clean Missing Files" task. # Emby removes ghost entries immediately — no user-facing file-not-found errors. # # ============================================================================================== # DESIGN PRINCIPLES # ============================================================================================== # # API as Ground Truth # What Sonarr tracks is authoritative. Files not in the API response are # orphans — Sonarr has no record of them and they serve no purpose. # The script never infers ownership from directory structure alone. # # Age Gate Before Deletion # Files under SONARR_ORPHAN_AGE are left alone regardless of tracked status. # Sonarr's import pipeline writes files before registering them — acting # immediately would delete files mid-import. # # Emby Cleanup Is Part of the Job # Deleting a file without telling Emby leaves ghost entries that show as # broken items. Triggering the Emby clean is not optional — it completes # the deletion from the user's perspective. # # ============================================================================================== # OPERATIONAL SAFEGUARDS # ============================================================================================== # # Seven gates — ALL must pass before any file is touched: # 1. Container running and not starting/unhealthy # 2. API reachable # 3. API version matches SONARR_VERSION_MAJOR in master.conf # 4. Series count > 0 # 5. Tracked file count > 0 # 6. Tracked count >= SONARR_MIN_TRACKED_PCT % of last known count # 7. Deletion size < SONARR_MAX_DELETE_GB — or --i-know-what-im-doing required # # acquire_lock "wait" — large scans take time, wait for previous run to finish # jq + curl validation — exits if either tool missing # ARR_DOCKER_TIMEOUT — container checks protected against daemon hangs (script-local, not common.sh's DOCKER_TIMEOUT) # notify_emby_scan() — triggers Emby clean after deletion # platform_require_cmd — notify script validated before use # Silent by default — orphans/junk warn(), clean library logs silently # # ============================================================================================== # STATE FILES # ============================================================================================== # # SONARR_TRACKED_COUNT_FILE — persistent baseline for the tracked % safety check (gate 6) # Updated after each successful run. Protects against misconfigured root path # returning an empty API response and deleting the entire library. # # ============================================================================================== # CONFIGURATION # ============================================================================================== # # host*.conf # # HOST*_SONARR_URL / HOST*_SONARR_API_KEY / HOST*_SONARR_TV_ROOT # HOST*_SONARR_PATH_MAP — container path → host path translation # All aliased by detect_hosts() — script uses unprefixed names # # master.conf # # SONARR_ORPHAN_AGE — days before untracked file eligible for deletion # SONARR_MAX_DELETE_GB — require --i-know-what-im-doing above this # SONARR_MIN_TRACKED_PCT — abort if tracked count drops below this % of last run # SONARR_TRACKED_COUNT_FILE — persistent baseline file path # SONARR_EXTENSIONS — video file extensions for orphan classification # SONARR_PROTECTED_PATTERNS — file patterns never deleted # SONARR_VERSION_MAJOR — expected Sonarr major version for API safety check # SONARR_IMPORT_SCAN_TIMEOUT — seconds to wait for pre-flight import scan (default 600) # ARR_CLEANUP_STATS — stats file path (read by coffee report) # # ============================================================================================== # RUNTIME MODES # ============================================================================================== # # sonarr_cleanup.sh — normal run # sonarr_cleanup.sh --dry-run — preview, no deletions # sonarr_cleanup.sh --log — verbose output # sonarr_cleanup.sh --status — show config and exit # sonarr_cleanup.sh --i-know-what-im-doing — bypass size threshold # sonarr_cleanup.sh --i-know-what-im-doing --skip-age-check — NUCLEAR MODE # # NUCLEAR MODE: both flags bypass age check AND size threshold. User accepts full # responsibility — the flag name is long and annoying by design. # # ============================================================================================== SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source "$SCRIPT_DIR/../load_config.sh" # ── Special flag pre-processing ─────────────────────────────────────────────────────────────── parse_destructive_flags "$@" parse_args "${FILTERED_ARGS[@]}" # ── Nuclear mode warning ────────────────────────────────────────────────────────────────────── nuclear_mode_warning # ============================================================================================== # ━━━ Setup ━━━ # ============================================================================================== if [[ "$EUID" -ne 0 ]]; then error "Must be run as root" exit 1 fi if ! command -v curl >/dev/null 2>&1; then error "curl not found — required for Sonarr API calls" exit 1 fi if ! command -v jq >/dev/null 2>&1; then error "jq not found — required for JSON parsing" notify "Sonarr cleanup failed on $(hostname) — jq not installed" "Sonarr Cleanup" "warning" exit 1 fi acquire_lock "wait" TMP_DIR="/tmp/sonarr_cleanup_$$" mkdir -p "$TMP_DIR" trap "_release_all_locks; rm -rf $TMP_DIR" EXIT if ! command -v docker &>/dev/null; then error "Docker command not found" exit 1 fi # detect_hosts() sets MY_ID and aliases SONARR_URL, SONARR_API_KEY, SONARR_TV_ROOT detect_hosts # Skip if Sonarr is not configured on this host if [[ -z "${SONARR_URL:-}" ]] || [[ -z "${SONARR_API_KEY:-}" ]]; then info "Sonarr not configured on $MY_ID ($LOCAL_SERVER_NAME) — skipping" exit 0 fi ARR_DOCKER_TIMEOUT=15 SONARR_CONTAINER="Sonarr" # Build path map from MY_ID's Sonarr path map build_arr_path_map "SONARR" require_var SONARR_URL require_var SONARR_API_KEY require_var SONARR_TV_ROOT if [[ ! -d "$SONARR_TV_ROOT" ]]; then error "TV root not found: $SONARR_TV_ROOT" notify "Sonarr cleanup failed on $(hostname) — TV root not found: $SONARR_TV_ROOT" \ "Sonarr Cleanup" "warning" exit 1 fi log "$ICON_GEAR Config: url=${SONARR_URL} root=${SONARR_TV_ROOT}" echo " $MY_ID ($LOCAL_SERVER_NAME) — $SONARR_URL" [[ "$DRY_RUN" == true ]] && warn "DRY RUN — no files will be deleted" [[ "$I_KNOW" == true ]] && warn "OVERRIDE — --i-know-what-im-doing active" [[ "$SKIP_AGE_CHECK" == true ]] && warn "OVERRIDE — --skip-age-check active — age check bypassed" # ============================================================================================== # ━━━ Status ━━━ # ============================================================================================== if [[ "$SHOW_STATUS" == true ]]; then echo "" echo "━━━━━ $ICON_SUMMARY STATUS ━━━━━" echo "$ICON_HOST Identity: $MY_ID ($LOCAL_SERVER_NAME)" echo "$ICON_GEAR Sonarr URL: $SONARR_URL" echo "$ICON_GEAR TV root: $SONARR_TV_ROOT" echo "$ICON_TIME Orphan age: ${SONARR_ORPHAN_AGE} days" echo "$ICON_GEAR Max delete: ${SONARR_MAX_DELETE_GB}GB (requires --i-know-what-im-doing)" echo "$ICON_GEAR Min tracked %: ${SONARR_MIN_TRACKED_PCT}%" echo "$ICON_GEAR Sonarr ver: v${SONARR_VERSION_MAJOR} expected" echo "$ICON_GEAR Extensions: ${SONARR_EXTENSIONS[*]}" echo "$ICON_GEAR Protected patterns: ${SONARR_PROTECTED_PATTERNS[*]}" echo "$ICON_GEAR Dry Run: $DRY_RUN" echo "$ICON_GEAR I know: $I_KNOW" echo "$ICON_GEAR Skip age check: $SKIP_AGE_CHECK" echo "━━━━━━━━━━━━━━━━━━━━━━━" exit 0 fi # ============================================================================================== # ━━━ Safety Layer 1 — Container Health ━━━ # ============================================================================================== echo "" echo "━━━ $ICON_SHIELD Safety Checks ━━━" check_container_health "$SONARR_CONTAINER" "$ARR_DOCKER_TIMEOUT" "Sonarr Cleanup" # ============================================================================================== # ── HELPER FUNCTIONS ────────────────────────────────────────────────────────────────────────── # ============================================================================================== # check_container_health(), arr_api(), has_extension(), matches_pattern_list(), format_bytes() — common.sh # ============================================================================================== # ━━━ Pre-flight: Sonarr Import Scan ━━━ # ============================================================================================== echo "" echo "━━━ $ICON_SYNC Pre-flight: Sonarr Import Scan ━━━" # Fetch root folders from Sonarr API and translate container paths to host paths mapfile -t SCAN_ROOTS < <( arr_api "$SONARR_URL" "$SONARR_API_KEY" "v3" "rootfolder" "Sonarr" | \ jq -r '.[].path' 2>/dev/null | \ while IFS= read -r cp; do translate_path "$cp"; done ) if [[ "${#SCAN_ROOTS[@]}" -eq 0 ]]; then error "No root folders returned from Sonarr API — aborting" notify "Sonarr cleanup aborted on $(hostname) — no root folders from API" \ "Sonarr Cleanup" "warning" exit 1 fi info "Scan targets (${#SCAN_ROOTS[@]}): ${SCAN_ROOTS[*]}" info "Triggering ProcessMonitoredDownloads pre-flight" SCAN_PAYLOAD='{"name": "ProcessMonitoredDownloads"}' trigger_and_await_command "$SONARR_URL" "$SONARR_API_KEY" "v3" "$SCAN_PAYLOAD" "${SONARR_IMPORT_SCAN_TIMEOUT:-600}" # ============================================================================================== # ━━━ Fetch Sonarr Tracked Files ━━━ # ============================================================================================== echo "" echo "━━━ $ICON_SYNC Fetching Sonarr Tracked Files ━━━" # Safety Layer 2 — API reachability if ! check_api "$SONARR_URL" "Sonarr" 10; then notify "Sonarr cleanup aborted on $(hostname) — API unreachable" "Sonarr Cleanup" "warning" exit 1 fi # Safety Layer 3 — API version check check_arr_version "$SONARR_URL" "$SONARR_API_KEY" "v3" "$SONARR_VERSION_MAJOR" "Sonarr" || exit 1 info "Querying Sonarr API..." # Fetch all series SERIES_RESPONSE=$(arr_api "$SONARR_URL" "$SONARR_API_KEY" "v3" "series" "Sonarr") || { error "Failed to fetch series from Sonarr" notify "Sonarr cleanup failed on $(hostname) — could not fetch series" \ "Sonarr Cleanup" "warning" exit 1 } SERIES_IDS=$(echo "$SERIES_RESPONSE" | jq -r '.[].id' 2>/dev/null) SERIES_COUNT=$(echo "$SERIES_IDS" | grep -c "." 2>/dev/null || echo 0) # Safety Layer 4 — series count > 0 if [[ "$SERIES_COUNT" -eq 0 ]]; then error "API returned 0 series — aborting to prevent mass deletion" notify "Sonarr cleanup aborted on $(hostname) — 0 series returned" \ "Sonarr Cleanup" "warning" exit 1 fi info "Found $SERIES_COUNT series — fetching episode files..." TRACKED_FILE="$TMP_DIR/tracked_paths.txt" > "$TRACKED_FILE" SERIES_INDEX=0 while IFS= read -r series_id; do [[ -z "$series_id" ]] && continue (( SERIES_INDEX++ )) [[ $(( SERIES_INDEX % 50 )) -eq 0 ]] && \ log "Fetching files: $SERIES_INDEX/$SERIES_COUNT series..." SERIES_FILES=$(arr_api "$SONARR_URL" "$SONARR_API_KEY" "v3" "episodefile?seriesId=${series_id}" "Sonarr" 2>/dev/null) if [[ -n "$SERIES_FILES" ]]; then while IFS= read -r api_path; do [[ -z "$api_path" ]] && continue translate_path "$api_path" >> "$TRACKED_FILE" done < <(echo "$SERIES_FILES" | jq -r '.[].path' 2>/dev/null) fi done <<< "$SERIES_IDS" sort -u "$TRACKED_FILE" -o "$TRACKED_FILE" # Build in-memory lookup map — O(1) per lookup vs O(n) grep per file declare -A TRACKED_MAP while IFS= read -r _tracked_path; do [[ -n "$_tracked_path" ]] && TRACKED_MAP["$_tracked_path"]=1 done < "$TRACKED_FILE" unset _tracked_path info "Built in-memory lookup map: ${#TRACKED_MAP[@]} tracked paths" TRACKED_COUNT=$(wc -l < "$TRACKED_FILE") # Safety Layer 5 — tracked count > 0 if [[ "$TRACKED_COUNT" -eq 0 ]]; then error "API returned 0 tracked files — aborting to prevent mass deletion" notify "Sonarr cleanup aborted on $(hostname) — 0 tracked files returned" \ "Sonarr Cleanup" "warning" exit 1 fi info "$SERIES_COUNT series | $TRACKED_COUNT tracked episode files" # Safety Layer 6 — percentage drop vs last known count check_tracked_count_floor "$TRACKED_COUNT" "$SONARR_TRACKED_COUNT_FILE" "$SONARR_MIN_TRACKED_PCT" "Sonarr Cleanup" # ============================================================================================== # ━━━ Scan TV Root ━━━ # ============================================================================================== echo "" echo "━━━ $ICON_CLEAN Scanning TV Root ━━━" info "Root: $SONARR_TV_ROOT | Orphan age: ${SONARR_ORPHAN_AGE} days" START=$(date +%s) ORPHAN_COUNT=0 JUNK_COUNT=0 RECENT_COUNT=0 PROTECTED_COUNT=0 ORPHAN_BYTES=0 JUNK_BYTES=0 AGE_SECONDS=$(( SONARR_ORPHAN_AGE * 86400 )) NOW=$(date +%s) while IFS= read -r filepath; do [[ -z "$filepath" ]] && continue if [[ -n "${TRACKED_MAP[$filepath]:-}" ]]; then log "TRACKED: $filepath" continue fi if matches_pattern_list "$filepath" "${SONARR_PROTECTED_PATTERNS[@]}"; then log "$ICON_PROTECTED PROTECTED: $filepath" (( PROTECTED_COUNT++ )) continue fi FILE_SIZE=$(stat -c%s "$filepath" 2>/dev/null || echo 0) if has_extension "$filepath" "${SONARR_EXTENSIONS[@]}"; then FILE_MTIME=$(stat -c %Y "$filepath" 2>/dev/null || echo 0) FILE_AGE=$(( NOW - FILE_MTIME )) if [[ "$FILE_AGE" -lt "$AGE_SECONDS" ]] && [[ "$SKIP_AGE_CHECK" != true ]]; then log "RECENT (skipping): $filepath" (( RECENT_COUNT++ )) continue fi warn "$ICON_TRASH ORPHAN: $filepath" (( ORPHAN_COUNT++ )) ORPHAN_BYTES=$(( ORPHAN_BYTES + FILE_SIZE )) else log "JUNK: $filepath" (( JUNK_COUNT++ )) JUNK_BYTES=$(( JUNK_BYTES + FILE_SIZE )) fi done < <( for host_path in "${SCAN_ROOTS[@]}"; do [[ -d "$host_path" ]] && find "$host_path" -type f 2>/dev/null done | sort -u ) TOTAL_DELETE_BYTES=$(( ORPHAN_BYTES + JUNK_BYTES )) TOTAL_REMOVED=$(( ORPHAN_COUNT + JUNK_COUNT )) # ============================================================================================== # ━━━ Safety Layer 7 — Deletion Size Threshold ━━━ # ============================================================================================== check_delete_size_threshold "$TOTAL_DELETE_BYTES" "$SONARR_MAX_DELETE_GB" "Sonarr Cleanup" # ── Execute Deletions ───────────────────────────────────────────────────────────────────────── if [[ "$DRY_RUN" == false ]]; then while IFS= read -r filepath; do [[ -z "$filepath" ]] && continue [[ -n "${TRACKED_MAP[$filepath]:-}" ]] && continue matches_pattern_list "$filepath" "${SONARR_PROTECTED_PATTERNS[@]}" && continue FILE_MTIME=$(stat -c %Y "$filepath" 2>/dev/null || echo 0) FILE_AGE=$(( NOW - FILE_MTIME )) if has_extension "$filepath" "${SONARR_EXTENSIONS[@]}"; then [[ "$FILE_AGE" -lt "$AGE_SECONDS" ]] && \ [[ "$SKIP_AGE_CHECK" != true ]] && continue fi rm -f "$filepath" 2>/dev/null || error "Failed to delete: $filepath" done < <( for host_path in "${SCAN_ROOTS[@]}"; do [[ -d "$host_path" ]] && find "$host_path" -type f 2>/dev/null done | sort -u ) info "Cleaning up empty folders..." for host_path in "${SCAN_ROOTS[@]}"; do [[ -d "$host_path" ]] && \ find "$host_path" -mindepth 1 -type d -empty -delete 2>/dev/null done info "Empty folders removed" fi END=$(date +%s) ORPHAN_HUMAN=$(format_bytes "$ORPHAN_BYTES") JUNK_HUMAN=$(format_bytes "$JUNK_BYTES") # ============================================================================================== # ━━━ Summary ━━━ # ============================================================================================== echo "" echo "━━━━━ $ICON_SUMMARY SONARR CLEANUP SUMMARY ━━━━━" echo "$ICON_HOST Identity: $MY_ID ($LOCAL_SERVER_NAME)" echo "$ICON_SYNC Tracked: $TRACKED_COUNT files ($SERIES_COUNT series)" echo "$ICON_SHIELD Protected: $PROTECTED_COUNT files (artwork, subtitles, metadata)" echo "$ICON_TRASH Orphans: $ORPHAN_COUNT files ($ORPHAN_HUMAN)" echo "$ICON_TRASH Junk: $JUNK_COUNT files ($JUNK_HUMAN)" echo "$ICON_SKIP Recent skipped: $RECENT_COUNT files (under ${SONARR_ORPHAN_AGE} days)" echo "$ICON_TIME Duration: $(format_duration $(( END - START )))" echo "" if [[ "$DRY_RUN" == true ]]; then warn "DRY RUN — no files deleted" elif [[ "$TOTAL_REMOVED" -eq 0 ]]; then echo "$ICON_DONE Clean — nothing to remove" else warn "$ICON_DONE Removed $TOTAL_REMOVED files (orphans: $ORPHAN_HUMAN junk: $JUNK_HUMAN)" notify "Sonarr cleanup on $(hostname) — removed $TOTAL_REMOVED files (orphans: $ORPHAN_HUMAN junk: $JUNK_HUMAN)" \ "Sonarr Cleanup" "warning" # Notify Emby to clean missing files — removes ghost entries immediately notify_emby_scan fi echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" # Write stats for sunday_morning_coffee_report.sh if [[ "$DRY_RUN" == false ]] && [[ -n "${ARR_CLEANUP_STATS:-}" ]]; then echo "$(date '+%Y-%m-%d')|sonarr|${ORPHAN_COUNT}|${ORPHAN_BYTES}|${JUNK_COUNT}|${JUNK_BYTES}|${RECENT_COUNT}|${TRACKED_COUNT}" \ >> "$ARR_CLEANUP_STATS" 2>/dev/null || true fi exit 0