Plex Auto-Delete Script

It’s not my first programming, but I am newer to Python. Honestly a lot of it just laziness and just creating something that works as people ask for new features. Some of the code is pretty obtuse because I’ve tried to keep the code as user-friendly as possible and the Plex Web API is not great and not at all documented. That’s seriously where most of my time goes.

The script works for python2 and python3 currently and I would like to keep it that way. This is done mostly by checking the python version where the two differ (mostly in string handling).

Hi Guys, Since you are restructuring the script, can you look into my request of adding the following:

“I would like the possibility to have an option that content gets deleted after all plex home members have watched it.
After a grace period etc.”

I think this option would make the script great and really usable for the Plex Home users among us.

Hi @Lusiada,

I added the ability to check watch status of users in Plex Home. It’s currently in the development branch as I haven’t fully tested it. If you don’t mind testing it, you could get the script from here:
https://github.com/ngovil21/Plex-Cleaner/archive/development.zip

or checkout the development branch if you know how to use git.

To setup you would either need to put all the home users you want to check the watch status of in default_homeUsers, or add a homeUsers setting to a Show Preference or Profile. The setting should have all the users separated by a common and wrapped in quotes like so:

homeUsers = “Billy,Bob,Joe”

The names need to match the usernames in PlexHome. From there it will look to delete episodes that are a minDays away from the time the last user watch the episode. You can also specify ‘all’ for homeUsers and it will check all the users in the PlexHome. This will include the Guest account if you have it enabled in Plex.

Hopefully it works well and I can merge it into the main branch.

If you have any problems could you PM me the contents of your log and config file.

Edit: I forgot to mention that PIN support is not included, and you will need to only provide the login info or token for the main account.

Hi ngovil21,

That is great news!! I am preparing a new server with a test (copy) Library to test it.
Will take some time but I will definitely get back to you with the results.

I don’t know what i’m doing bad but the script don’t delete for me i get this message at the end:



            Summary -- Script Completed Successfully

Total File Count 360
Kept Show Files 359
On Deck Files 5
Deleted Files 0
Moved Files 0
Copied Files 0
Flagged Files 0
Rescanned Sections



Sorry to post this here i have my Cleaner.conf

{
“Host”: “”,
“Port”: “”,
“SectionList”: [],
“IgnoreSections”: [],
“LogFile”: “”,
“trigger_rescan”: false,
“Token”: “”,
“Username”: “”,
“Password”: “”,
“Shared”: false,
“DeviceName”: “”,
“RemoteMount”: “”,
“LocalMount”: “”,
“plex_delete”: true,
“similar_files”: true,
“cleanup_movie_folders”: true,
“minimum_folder_size”: 0,
“default_episodes”: 0,
“default_minDays”: 0,
“default_maxDays”: 0,
“default_action”: “delete”,
“default_watched”: true,
“default_location”: “”,
“default_onDeck”: true,
“ShowPreferences”: {
“Show 1”: {
“minDays”: 0,
“maxDays”: 0,
“onDeck”: true,
“episodes”: 0,
“location”: “/path/to/folder”,
“action”: “delete”,
“watched”: true
},
“Show 2”: {
“minDays”: 0,
“maxDays”: 0,
“onDeck”: false,
“episodes”: 0,
“location”: “/path/to/folder”,
“action”: “delete”,
“watched”: true
},
“Show 3”: {
“action”: “delete”
},
“Show Preferences”: {}
},
“MoviePreferences”: {
“action”: “delete”,
“location”: “”,
“minDays”: 0,
“onDeck”: true,
“watched”: true
},
“Profiles”: {
“Profile 1”: {
“onDeck”: true,
“maxDays”: 0,
“minDays”: 0,
“episodes”: 0,
“location”: “/path/to/folder”,
“action”: “delete”,
“watched”: true
}
},
“Version”: 1.8
}

@eskwire
Are you using Plex Home? If you are, you need to have a Token or login information in the script. You also have the setting plex_delete enabled. There actually is a bug there that I have fixed, but it’s currently in the development tree while I test multi-users. You should see an error in the log for not being able to connect to the host.

I merged the fix into the master branch. Please try the new version.

@ngovil21 I add the token to the conf file but no delete only flagging.

also downloaded the last version.

here the conf, what am doing rung:

{
“Host”: “”,
“Port”: “”,
“SectionList”: [],
“IgnoreSections”: [],
“LogFile”: “”,
“trigger_rescan”: true,
“Token”: “########”,
“Username”: “”,
“Password”: “”,
“Shared”: false,
“DeviceName”: “”,
“RemoteMount”: “”,
“LocalMount”: “”,
“plex_delete”: true,
“similar_files”: true,
“cleanup_movie_folders”: true,
“minimum_folder_size”: 30,
“default_episodes”: 0,
“default_minDays”: 0,
“default_maxDays”: 0,
“default_action”: “delete”,
“default_watched”: true,
“default_location”: “”,
“default_onDeck”: true,
“default_homeUsers”: “”,
“ShowPreferences”: {
“Show 1”: {
“minDays”: 10,
“maxDays”: 30,
“onDeck”: true,
“episodes”: 3,
“location”: “/path/to/folder”,
“action”: “delete”,
“watched”: true
},
“Show 2”: {
“minDays”: 10,
“maxDays”: 30,
“onDeck”: false,
“episodes”: 0,
“location”: “/path/to/folder”,
“action”: “delete”,
“watched”: false
},
“Show 3”: {
“action”: “keep”
},
“Show Preferences”: {}
},
“MoviePreferences”: {
“action”: “delete”,
“location”: “”,
“minDays”: 0,
“onDeck”: true,
“watched”: true
},
“Profiles”: {
“Profile 1”: {
“maxDays”: 30,
“episodes”: 3,
“location”: “/path/to/folder”,
“action”: “delete”,
“watched”: true,
“onDeck”: true,
“minDays”: 10,
“homeUsers”: “”
}
},
“Version”: 1.9
}

Could you PM me the contents of your log? You can redact the Token and config information, but I want to see what’s going on. Also make sure you are NOT running the Script with --test as an argument. That will only flag files for testing purposes.

Edit: Don’t run the script with --test if you are ready for the script to delete files.

Has anyone addressed the following error? Seems like you need to authenticate first to be able to perform these actions:


                       Detected Settings

Host: 127.0.0.1
Port: 32400
Section: 6
URL: http://127.0.0.1:32400/library/sections/6/recentlyViewed
OnDeck URL: http://127.0.0.1:32400/library/sections/6/onDeck
Show Count: 1
Number of Shows Detected For Keeping: 0
Shows to Keep: | (None Listed) |
Delete: Disabled - Flagging Only
Delete OnDeck: No
Operating System: Linux
Traceback (most recent call last):
File “./auto_cleanup-recorded_tv.py”, line 109, in
doc = xml.dom.minidom.parse(urllib2.urlopen(URL))
File “/usr/lib64/python2.7/urllib2.py”, line 127, in urlopen
return _opener.open(url, data, timeout)
File “/usr/lib64/python2.7/urllib2.py”, line 410, in open
response = meth(req, response)
File “/usr/lib64/python2.7/urllib2.py”, line 523, in http_response
‘http’, request, response, code, msg, hdrs)
File “/usr/lib64/python2.7/urllib2.py”, line 448, in error
return self._call_chain(*args)
File “/usr/lib64/python2.7/urllib2.py”, line 382, in _call_chain
result = func(*args)
File “/usr/lib64/python2.7/urllib2.py”, line 531, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 401: Unauthorized

Info on my server:

uname -a

Linux plex.xyz.com 3.10.0-123.20.1.el7.x86_64 #1 SMP Thu Jan 29 18:05:33 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

cat /etc/system-release

CentOS Linux release 7.0.1406 (Core)

@vinnielima888
It looks like you may be using an older version of the script. Could you try looking and running the script from here my GitHub?

This version has support for authenticating on PlexHome, which is required on current versions of PMS. You will need to update some of the settings as there are more features now. Explanations for settings are available in the script and on the GitHub Wiki.

@ngovil21 can you implement a looping functionality in Plex Cleaner, so that the script falls into sleep when complete, and reruns the process after X minutes (default would be 360, 480 or 720). Right now I have to use FireDaemon and setup a schedule to restart each 12 hours. I think this is fairly simple to implement and not require paid tools like FireDaemon to keep alive on the system.

@apoapostolov You shouldn’t have to use FireDaemon to run the script. Windows Task Scheduler should work fine or cron on Linux/Mac. I’ll look into making a daemon version of the script, but the other options are better than having a lingering program running. Daemons are usually better for scripts that are waiting for things to be done.

First, let me say thanks for all the hard work in putting this together. I have used this script in the past and haven’t checked back in awhile. Is there a quick run down of what to put for the settings? I understand some of it, but not sure on others.

For “RemoteMount” and “LocalMount” what path is suppose to go there? I run Plex Server on a Mac but my file server is running on another computer running Ubuntu Server.

How does the Profile section work?

Thanks again for everything!

Sure explanations for the settings can be found inside the script, just open it in notepad, or they can be found on the GitHub Wiki:

RemoteMount and LocalMount are only to be used if you are running the plex script, from a computer that is different from the plex server, and to convert the remote plex server’s media directory to the local computer’s directory where media is located. If you are running the script where Plex Media Server is running, your Mac, then you should leave both of those options blank. If you want to run your script on the File Server, then you would need to put the media directory as referenced by the Mac as the RemoteMount, and the media directory on the File Server as the local mount. If your media files aren’t under one media directory, then you won’t be able to use this option, and must run it on the Mac. As long as the Plex Server has delete privileges, it is recommended you run the script on the Plex Server.

Profiles allow you to manage the Auto-Delete settings through the PlexWeb interface. Rather than setting individual show settings using Show Preferences, you can create a Profile and then add the name of the profile to the Collections tag of the show. Make sure profile names don’t clash with any collections you have already have set. Also the behavior may not be consistent if you try to put a show in two Profiles, so keep shows in only one Profile. In general I would keep the Profile Collections separate from your organizing Collections.

This looks great (especially the profiles feature!) but, unless I’m mistaken, keeping a series in the deck requires keeping at least one watched episode around (it certainly used to be the case, has that changed?).

However, I can’t get the script to keep one watched episode for a series unless it’s the only episode, and I’d like to do this regardless of how many unwatched episodes there are. So, I’m not sure if I’m missing something, if things changed in Plex to make this irrelevant, or if I’m asking for an enhancement…

@d.g. said:
This looks great (especially the profiles feature!) but, unless I’m mistaken, keeping a series in the dock requires keeping at least one watched episode around (it certainly used to be the case, has that changed?).

However, I can’t get the script to keep one watched episode for a series unless it’s the only episode, and I’d like to do this regardless of how many unwatched episodes there are. So, I’m not sure if I’m missing something, if things changed in Plex to make this irrelevant, or if I’m asking for an enhancement…

The script used to look at the last 100 recently added items. This is no longer the case. As long as you are using the newest version from GitHub, it should go through all shows or movies in a section now and then will process them based on the settings. If the action for the show is ‘keep’ it will be skipped, otherwise it be processed.

@ngovil21 I think we’re talking about different things.

Let’s say I have an entire season of something I want to delete when watched and I’d like it to stay in the deck as I’m watching it. So, while deleting watched episodes I want to always keep one watched episode of that show (as well as the unwatched episodes). I don’t see a way to do this.

@d.g. said:
@ngovil21 I think we’re talking about different things.

Let’s say I have an entire season of something I want to delete when watched and I’d like it to stay in the dock as I’m watching it. So, while deleting watched episodes I want to always keep one watched episode of that show (as well as the unwatched episodes). I don’t see a way to do this.

Have you set the episodes to 1 for that show and have watched set to true for that show?
Ex:

ShowPreferences = {
“Show 1”: {"episodes": 1, “watched”: True, “action”: “delete”, “maxDays”: 30}
}

You can then set maxDays as a maximum amount of time to finally delete something or 0 to ignore it. If these are your settings, then there must be a bug and I will look into it.

(I’m using flag, and have set min and max days to 0 to not confuse things further just now, but…)

Yes: with watched set to true, “episodes”: 1 is only keeping one watched episode if there are no unwatched episodes. Otherwise it’s flagging every watched episode for deletion and keeping the unwatched episodes. Latest github version from the master branch.

Okay I understand what you are saying now. You want to save one watched episode, even when there are unwatched episodes left. Can you use minDays as an acceptable substitute for your situation? Otherwise you seem to have a very fringe case and I don’t want to include an extra option for just one person. If watched is set to true, minDays will calculate from the last watched time.