Plex Auto-Delete Script

Hi all.

New user of the script here encountering a puzzling problem.

I’ve got the config file customized and it works exactly as configured when I run it manually. As in I have everything in a C:\Python folder, I CD to it and manually run Python.exe PlexCleaner.py and everything works and the appropriate files are deleted (relevant portions of log below)…

INFO:root:American Gothic - S01xE003 - Nighthawks | Viewed: 1 | Days Since Last Viewed: 0 | On Deck: False
INFO:root:Finding files similar to: Z:\TV\American Gothic\Season 01\American Gothic - s01e03 - Nighthawks*
INFO:root:**[DELETED] Z:\TV\American Gothic\Season 01\American Gothic - s01e03 - Nighthawks.mp4

INFO:root:----------------------------------------------------------------------------
INFO:root:----------------------------------------------------------------------------
INFO:root: Summary – Script Completed Successfully
INFO:root:----------------------------------------------------------------------------
INFO:root:
INFO:root: Total File Count 86
INFO:root: Kept Show Files 85
INFO:root: On Deck Files 0
INFO:root: Deleted Files 1
INFO:root: Moved Files 0
INFO:root: Copied Files 0
INFO:root: Flagged Files 0
INFO:root: Rescanned Sections 2
INFO:root:
INFO:root:----------------------------------------------------------------------------
INFO:root:----------------------------------------------------------------------------

In order to schedule the deletion of watched shows I created a batch file that basically replicates what I’m doing manually…

cd \Python
Python.exe PlexCleaner.py

I then have a scheduled task (which runs with the exact same account credentials as the account I’m logged in as when I run the command manually) that simply runs the batch file every morning. When the scheduled task runs (it actually ran earlier this morning before I ran the commands manually as shown above) it logs this…

INFO:root:American Gothic - S01xE003 - Nighthawks | Viewed: 1 | Days Since Last Viewed: 0 | On Deck: False
INFO:root:[NOT FOUND] Z:\TV\American Gothic\Season 01\American Gothic - s01e03 - Nighthawks.mp4

INFO:root:----------------------------------------------------------------------------
INFO:root:----------------------------------------------------------------------------
INFO:root: Summary – Script Completed Successfully
INFO:root:----------------------------------------------------------------------------
INFO:root:
INFO:root: Total File Count 86
INFO:root: Kept Show Files 85
INFO:root: On Deck Files 0
INFO:root: Deleted Files 0
INFO:root: Moved Files 0
INFO:root: Copied Files 0
INFO:root: Flagged Files 0
INFO:root: Rescanned Sections
INFO:root:
INFO:root:----------------------------------------------------------------------------
INFO:root:----------------------------------------------------------------------------

I’m running Windows 10. It’s almost like the scheduled task is running into a rights issue but the task is configured to use the exact same credentials and with highest privileges. So, everything is working great except I can’t get it to work via scheduled task.

Any ideas?

@Nilex1 I think the file was deleted, but your Plex Library hasn’t updated yet. Try doing a refresh of your Plex Library and then running the Task through the Task Scheduler.

The Rescan Plex Library option might not be working.

Thanks for the information. I just tested your theory and there is still something else causing the issue.

I found an unwatched TV episode, manually marked it as watched then manually refreshed the library and waited 30 minutes. I then ran the scheduled task on demand and this was logged…

INFO:root:Home Free - S02xE003 - Pressure’s On | Viewed: 1 | Days Since Last Viewed: 0 | On Deck: False
INFO:root:[NOT FOUND] Z:\TV\Home Free\Season 02\Home Free - s02e03 - Pressure’s On.mp4
INFO:root:

So it correctly shows the show as watched but still couldn’t delete it. I verified the .mp4 does indeed still exist at the location specified which makes sense since it isn’t being logged as deleted, but as not found.

For reference this is how my config file is set up…

“Host”: “127.0.0.1”,
“Port”: “32400”,
“SectionList”: [2],
“IgnoreSections”: [1],
“LogFile”: “C:\Python\PlexCleaner.log”,
“LogFileMode”: “overwrite”,
“trigger_rescan”: true,
“Token”: “mytoken”,
“Username”: “”,
“Password”: “”,
“Shared”: false,
“DeviceName”: “”,
“RemoteMount”: “”,
“LocalMount”: “”,
“plex_delete”: false,
“similar_files”: true,
“cleanup_movie_folders”: true,
“minimum_folder_size”: 30,
“default_episodes”: 0,
“default_minDays”: 0,
“default_maxDays”: 60,
“default_action”: “delete”,
“default_watched”: true,
“default_progressAsWatched”: 0,
“default_location”: “”,
“default_onDeck”: true,
“default_homeUsers”: “”,
“default_ignoreFolders”: [],

I believe I’ve resolved the problem so a little more detail about my setup.

I’m running PMS on a desktop PC (hope to move to Shield TV as the PMS soon but it still has some issues) and all my media is on a WD MyCloud NAS (hence the Z: mapped drive in the path).

I switched the plex_delete flag from the default of false to true and ran the scheduled task and it successfully deleted the file.

For my reference, any time the media is NOT stored directly on the device running PMS does the plex_delete flag need to be true?

@Nilex1 I think your issue was a permissions problem. Even though you ran the task with highest privileges, it still didn’t have the right permissions. The permissions are not as clear on Windows. I think tasks are run solely as the user without any groups such as Administrator. You can try looking on your right clicking on the Z Drive and make sure the username has read and right permissions on the drive.

The plex_delete flag just uses the Plex API to delete the file. It should work just fine to get around some permission problems. similar_files can’t be used with plex_delete, but that just cleans up some metadata files if they exist.

@ngovil21

Amazing script, thanks for all the hard work :). I’m having a small problem trying to understand the whole PlexHome sharing setup and how it works with your script.

I have a Plex Server belonging to a non plex pass user and have sharing enabled to friends who are also non plex pass users.

The functionality I was hoping of achieving is a way of only deleting videos that have been watched by the plex server owner and all the friends who share that same video.

Does your script support such functionality?

@BhavikRamsundar I don’t think the script will handle the setup you want. The script does not check the watch status of multiple Shared Users because they require a different authentication token. I don’t plan on implementing this feature due to security reasons.

What I do is have the script delete shows three weeks after I have watched them. This generally is enough time for anyone who keeps up with a show to watch it.

Ahh, I see.

I would like to make some changes to the code when I can to try and
implement it.

As for the security concerns, if it’s token based and just shared amongst
users, I don’t see how it would be a major concern since we already sharing
libaries with users.

I’m encountering one small issue. I want to keep only MR. ROBOT episodes until further notice. I know there is a default_ignoreFolders variable.

I’ve tried setting this variable to both the mapped drive location (i.e. Z:\TV\Mr. Robot\Season 2) and the UNC location (\MYCLOUD\TV\Mr. Robot\Season 2) and the episodes are still being deleted.

What am I doing wrong?

@BhavikRamsundar It wouldn’t be too hard to add. You can probably create a dict with the username and token and add that into the current homeuser check. I don’t wish to add it because I don’t want to support users with any issues.

@Nilex1 Can you try double \\ in the location so try Z:\\TV\\Mr. Robot\\Season 2
Alternatively you can set a Show Preference for Mr. Robot with action set to keep. Please check the wiki for more information.

@Nilex1 Can you try double \\ in the location so try Z:\\TV\\Mr. Robot\\Season 2
Alternatively you can set a Show Preference for Mr. Robot with action set to keep. Please check the wiki for more information.

Double slashes resolved the issue. Thanks!

this script has stopped working for tv shows for me, it detects everything as unwachted

@m0ul said:
this script has stopped working for tv shows for me, it detects everything as unwachted

What OS are you on and what is the version of your Plex Media Server? Did it recently update?

i running plex version 1.0.3.2461 on a synology.

@m0ul said:
i running plex version 1.0.3.2461 on a synology.

I just upgraded to the latest Plex version (1.0.3.2461) and it’s working for me on Ubuntu, which should be similar to synology.

Have you tested the script by marking an episode as watched, and running with the --test flag?

tried that, does not change anything

it seems if remove my user name and password from the config flie. then it sees everything as watched again.

@m0ul said:
it seems if remove my user name and password from the config flie. then it sees everything as watched again.

So it’s working now? If you’re still having an issue, could you PM me your config file with personal information removed?

Sorry, n00b here. i have a very simple use case for this. Basically if it is in the “TV Shows” library, watched more than two days ago, and not on deck, then it should be deleted. Can someone help me with a config for that?

@tchawes said:
Sorry, n00b here. i have a very simple use case for this. Basically if it is in the “TV Shows” library, watched more than two days ago, and not on deck, then it should be deleted. Can someone help me with a config for that?

Ya, copy the Cleaner.conf.default file to a new file called Cleaner.conf.

You will need to edit the Cleaner.conf file and change the Host setting if you are running the script from a different computer than your Plex Media Server. Then you will need to add your Plex username and password, so that the script can get information from your Plex Server.

Edit the file and change the SectionList configuration to include “TV Shows” or find the id of the section through plex. Set default_minDays to 2, default_action to ‘delete’, default_watched to true and default_onDeck to true.

Then run the script in test mode first using python.exe PlexCleaner.py --test or however you would for your OS and verify the log is FLAGGING the correct files. The script will also print a token that you can use for logging in, if you don’t want to keep your username and password saved in the Cleaner.conf file.

That should mostly do it. If you have an issue, PM your Cleaner.conf file (rename it .txt before you do)