Skip to content

Add --delete-mode Argument for Advanced Deletion Control (plex, hard-delete, both)#82

Open
samestrin wants to merge 1 commit intol3uddz:masterfrom
samestrin:feature/cli-delete-mode
Open

Add --delete-mode Argument for Advanced Deletion Control (plex, hard-delete, both)#82
samestrin wants to merge 1 commit intol3uddz:masterfrom
samestrin:feature/cli-delete-mode

Conversation

@samestrin
Copy link

Key Enhancements:

  1. Advanced Deletion Control
  • New --delete-mode CLI flag:
    • plex (default): delete from Plex via API only.
    • hard-delete: delete media files directly from the filesystem.
    • both: delete via Plex API and filesystem.
  • Also configurable via DEFAULT_DELETE_MODE in config.py.
  1. Refactored delete_item Function
  • Complete overhaul with logic delegated to:
    • _get_file_paths_for_media_id
    • _handle_movie_deletion
    • _handle_episode_deletion
  • Smarter handling of:
    • Movies: deletes either a specific file or parent directory.
    • Episodes: deletes individual media files.
  1. Improved Plex API Use
  • More precise deletion using endpoints like library/metadata/{media_id}.
  • Adds verify_item_deleted() to confirm API-based deletions.
  • Fallback logic: if Plex deletion fails in plex mode, falls back to hard-delete.

Addition Enhancements:

  1. Improved Structure
  • part_info['item_type'] added to support correct strategy selection.
  • process_later dictionary is now globally accessible to helper functions.
  1. New Standard Library Dependencies
  • argparse: handles the new CLI option.
  • shutil: used for reliable directory removal via shutil.rmtree.
  1. Logging & Error Handling
  • More granular logs and structured exception handling throughout the deletion logic.

Use Case:
This feature is especially useful for users running Plex in read-only containers where API deletes are disabled. By running plex_dupefinder from a sidecar container with filesystem write access, users can safely and efficiently clean up duplicates directly.

1. Advanced Deletion Control & Functionality:

   - The delete_item function has been completely overhauled.
   - Deletion Modes: Introduces a mode parameter ( 'plex' , 'hard-delete' , 'both' ) to control deletion behavior:
     - plex : Deletes only from the Plex server's database.
     - hard-delete : Deletes files directly from the filesystem.
     - both : Deletes from Plex and the filesystem.
   - Command-Line Argument: You can now specify the deletion mode using the --delete-mode argument (e.g., python plex_dupefinder.new.py --delete-mode both ). It can also be configured via DEFAULT_DELETE_MODE in config.py .
   - Filesystem Deletion Logic:
     - New helper functions ( _get_file_paths_for_media_id , _handle_movie_deletion , _handle_episode_deletion ) manage file removal.
     - For movies, it intelligently deletes either the specific file (if in a shared directory) or the entire movie's parent directory.
     - For episodes, it deletes the specific media file.
   - Plex API Endpoint Usage: Uses more precise Plex API endpoints for deletion (e.g., library/metadata/{media_id} for movies, which is generally more appropriate for removing the entire item).
   - Deletion Verification: Includes a verify_item_deleted function to confirm that items are actually removed from Plex after an API delete call.
   - Fallback Hard Delete: If Plex API deletion fails when in 'plex' mode, the script will attempt a hard delete from the disk.

Addition Enhancements:

1. Improved Structure and Data Handling:

   - The item.type (e.g., 'movie', 'episode') is now stored within the part_info dictionary (as part_info['item_type'] ). This is used by the new delete_item function to determine the correct deletion strategy.
   - The process_later dictionary is initialized globally, allowing helper functions to access it for retrieving file paths.

2. New Dependencies (Standard Libraries):

   - Imports argparse for handling command-line arguments.
   - Imports shutil for more robust directory removal ( shutil.rmtree ).

3. Logging and Error Handling:

   - The new deletion logic includes more detailed logging and error handling.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant