A small Bash script to automatically remove watched YouTube videos from a Plex library.
This script was created for a very specific use case:
- I download YouTube videos using the open-source tool ytdl-sub.
- Those downloads are added into Plex as a dedicated "YouTube" library (of type TV Shows).
- To keep Plex uncluttered, I wanted a way to automatically delete watched episodes while keeping unwatched ones intact.
- The script is designed to run on Unraid with a Plex installation, where the available system packages are limited.
Therefore, it has minimal dependencies: onlybash,curl, andjqare required (both included in Unraid by default).
- Connects to your Plex server via the Plex API.
- Identifies the library section by name (
YouTubeby default). - Lists all watched episodes (viewCount > 0).
- Runs in dry-run mode by default – nothing is deleted until you explicitly enable it.
- Optionally deletes the episodes directly from Plex once confirmed.
- Outputs clean, human-readable logs of what would/will be removed.
- Leaves unwatched or partially-watched episodes untouched.
- Plex Media Server with "Allow media deletion" enabled in the settings.
- Dependencies:
bash,curl,jq. - A valid Plex token and the server's base URL.
- Copy the script to your Unraid server (or any machine with network access to Plex).
- Make it executable:
chmod +x plex-delete-watched-youtube.sh- Run in dry-run mode (default):
./plex-delete-watched-youtube.shThis will list all watched YouTube episodes that would be deleted.
- Actually delete them:
./plex-delete-watched-youtube.sh --delete- You’ll be prompted for confirmation.
To skip the prompt:
./plex-delete-watched-youtube.sh --delete --yesThe script uses sensible defaults but can be overridden with environment variables or CLI arguments.
PLEX_URL – Plex server URL (default: http://YOURIP:32400)
PLEX_TOKEN – Your Plex auth token
LIB_TITLE – Library title (default: YouTube)
PAGE_SIZE – API fetch size (default: 200)
Example:
PLEX_URL="http://myserver:32400" \
PLEX_TOKEN="abc123" \
LIB_TITLE="YouTube" \
./plex-delete-watched-youtube.sh --delete --yesThis project is released under the GPL-3.0. Contributions, forks, and improvements are welcome!