# Plex Auto-Delete Script

**URL:** <https://forums.plex.tv/t/plex-auto-delete-script/45826>\
**Category:** Dev/API Corner\
**Tags:** other-dev\
**Created:** [October 31, 2013, 6:36pm UTC](https://forums.plex.tv/t/plex-auto-delete-script/45826 "2013-10-31T18:36:27Z")\
**Posts on this page:** 20\
**Page:** 5

<div class="post-metadata">

**Author:** ![brotbuexe](https://avatars.discourse-cdn.com/v4/letter/b/6f9a4e/32.png) [@brotbuexe](https://forums.plex.tv/u/brotbuexe)\
**Post date:** [June 27, 2014, 6:36am UTC](https://forums.plex.tv/t/plex-auto-delete-script/45826/81 "2014-06-27T06:36:31Z")

</div>

I tried the version Last Updated: 02/26/2014 5:05PM MST on Windows Python v3.3.2&nbsp;and I get this:

C:\TOOLS\>c:\Python33\python.exe autodel.py

&nbsp; File "autodel.py", line 154

&nbsp; &nbsp; except Exception, e:

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ^

SyntaxError: invalid syntax

---

<div class="post-metadata">

**Author:** ![jam3se](https://avatars.discourse-cdn.com/v4/letter/j/ec9cab/32.png) [@jam3se](https://forums.plex.tv/u/jam3se)\
**Post date:** [July 4, 2014, 8:11am UTC](https://forums.plex.tv/t/plex-auto-delete-script/45826/82 "2014-07-04T08:11:22Z")

</div>

Anyway for this to delete files once they have been CloudSync'ed?

---

<div class="post-metadata">

**Author:** ![nicmart](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/nicmart/32/104042_2.png) [@nicmart](https://forums.plex.tv/u/nicmart)\
**Post date:** [July 20, 2014, 1:22pm UTC](https://forums.plex.tv/t/plex-auto-delete-script/45826/83 "2014-07-20T13:22:28Z")

</div>

> I tried the version Last Updated: 02/26/2014 5:05PM MST on Windows Python v3.3.2&nbsp;and I get this:
> 
> C:\TOOLS\>c:\Python33\python.exe autodel.py
> 
> &nbsp; File "autodel.py", line 154
> 
> &nbsp; &nbsp; except Exception, e:
> 
> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ^
> 
> SyntaxError: invalid syntax

I had the same issue but using the original code on page 1 solved it for me.

---

<div class="post-metadata">

**Author:** ![eskwire](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/eskwire/32/3842_2.png) [@eskwire](https://forums.plex.tv/u/eskwire)\
**Post date:** [July 20, 2014, 7:17pm UTC](https://forums.plex.tv/t/plex-auto-delete-script/45826/84 "2014-07-20T19:17:41Z")

</div>

Thats a syntax problem meaning he is not reading a character the right way because its rung.&nbsp;

---

<div class="post-metadata">

**Author:** ![brotbuexe](https://avatars.discourse-cdn.com/v4/letter/b/6f9a4e/32.png) [@brotbuexe](https://forums.plex.tv/u/brotbuexe)\
**Post date:** [July 23, 2014, 5:28am UTC](https://forums.plex.tv/t/plex-auto-delete-script/45826/85 "2014-07-23T05:28:05Z")

</div>

I see, the original script was tested on windows with python 3.32. The latest looks like is not compatible with 3.3.

I tried to convert it with 2to3 and fixed some errors. It runs through but I doubt it is all working.

Which python version should be used on windows to run the version from february?

---

<div class="post-metadata">

**Author:** ![Steven4x4](https://avatars.discourse-cdn.com/v4/letter/s/838e76/32.png) [@Steven4x4](https://forums.plex.tv/u/Steven4x4)\
**Post date:** [July 23, 2014, 7:40am UTC](https://forums.plex.tv/t/plex-auto-delete-script/45826/86 "2014-07-23T07:40:13Z")

</div>

Brotbuexe,

I originally coded this for both Linux and Windows.

It was odd to find something didn't work in Linux when it workedin windows and vice versa... Turned out it was a Python Version issue and not an OS issue at all.

With the original code, I wrote it for 2.7.3 Linux. I tried to port it over to 3.3.2 for windows as that was the latest version, but certian functions didn't work.

So I ended up coding a detection tool to detect linux vs windows. Well I ended up on a windows box, but it was running some python scripts with 2.7.3 and I tried to run my script on it and ran into errors.

Ended up porting all the code I wrote for linux section into the windows and worked just fine. So it really turned out to be Python version issue.

Primarlly with these lines of codes listed below... So if auto detect OS doesn't work use L in the OS settings. Remember to NOT set delete... It will create you a log file so you can see everything the script is doing if it executes successfully or everthing its going to do (log only).

Just follow the instructions in the first thread.

P.S. Your windows machine is fully capable of running 2 different versions of python side by side (2.7.3 and 3.3.2), just point the path to the correct python executable you want to use. Hope this helps.

To Everyone:

Sorry guys, haven't had anytime to even look at this or work on it.

I run a gaming organization and barely have time for that anymore either.

Thank you everyone who contributed to this thread and to coding.

I'll stay touch and help when I can.

Anyways the lines of code in question are here... the urllib2 exists in one but not the other where you'd then use the urllib.

Again, originally thought it was a Linux vs Windows issue. Turned out to be a 2.7.3 vs 3.3.2 issue.

```
if PC=="L":
  print("Operating System: Linux " + AD)
  import urllib2
  doc = xml.dom.minidom.parse(urllib2.urlopen(URL))
  deck = xml.dom.minidom.parse(urllib2.urlopen(OnDeckURL))
elif PC=="W":
  print("Operating System: Windows " + AD)
  import urllib.request
  doc = xml.dom.minidom.parse(urllib.request.urlopen(URL))
  deck = xml.dom.minidom.parse(urllib.request.urlopen(OnDeckURL))
else:
```

The better way to have done it is either detect the Python Version vs the OS. Or detect whether the module exists OR try to import it and if it fails import the other... ETC..

---

<div class="post-metadata">

**Author:** ![mordamus](https://avatars.discourse-cdn.com/v4/letter/m/e480ec/32.png) [@mordamus](https://forums.plex.tv/u/mordamus)\
**Post date:** [July 23, 2014, 5:44pm UTC](https://forums.plex.tv/t/plex-auto-delete-script/45826/87 "2014-07-23T17:44:34Z")

</div>

This my alterd code  
- working on windows  
- deleting of similair files  
- working with python 3.3  
&nbsp;  
&nbsp;

> #!/usr/bin/python  
> # -\*- coding: utf-8 -\*-  
> ####################################################################################  
> ##&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; INFORMATION  
> ####################################################################################  
> ##&nbsp;&nbsp; Developed by: Steven Johnson&nbsp; (minor modifications by egretsareherons)  
> ##&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (a minor modifications by aross01)  
> ##&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (a minor modification by STEEVo)  
> ##  
> ##&nbsp;&nbsp; Last Updated: 02/26/2014 5:05PM MST  
> ##  
> ##&nbsp;&nbsp; Description: Auto-Delete Watched Items In Plex  
> ##  
> ##&nbsp;&nbsp; Required Configurations:  
> ##&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - PC&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (Blank = AutoDetect&nbsp; |&nbsp; W = Windows&nbsp; |&nbsp; L = Linux/Unix/MAC)  
> ##&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - Host&nbsp;&nbsp;&nbsp;&nbsp; (Hostname or IP&nbsp; | Blank = 127.0.0.1)  
> ##&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - Port&nbsp;&nbsp;&nbsp;&nbsp; (Port&nbsp; |&nbsp; Blank = 32400)  
> ##&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - SectionList - A list of sections to operate on.&nbsp; If this is blank, the script  
> ##&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; will get a list of all sections from the server & operate on all, except  
> ##&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for the sections listed in IgnoreSections  
> ##&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - IgnoreSections - Only used if SectionList is blank. When SectionList is blank,  
> ##&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; the&nbsp; script will operate on all available sections of your install  
> ##&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; except for the ones listed here.  
> ##&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - Delete&nbsp;&nbsp; (1 = Delete&nbsp; |&nbsp; Blank = 0 (For Testing or Review))  
> ##&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - Move&nbsp;&nbsp;&nbsp;&nbsp; (1 = move files to DestinationDir, 0 = don't move)  
> ##&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - Copy&nbsp;&nbsp;&nbsp;&nbsp; (1 = copy files to DestinationDir, 0 = don't copy)  
> ##&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - Shows&nbsp;&nbsp;&nbsp; ( ["Show1","Show2"]; = Kept Shows OR [""]; = DEL ALL SHOWS )  
> ##&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - OnDeck&nbsp;&nbsp; ( 1 = Keep If On Deck&nbsp; |&nbsp; Blank = Delete Regardless if onDeck&nbsp; )  
> ##&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - DestinationDir&nbsp; - the place to move or copy your watched files for later manual deletion  
> ##&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - DestinationSection - if your destination dir is also a configured section, set the  
> ##&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; number here so TriggerRescan knows to rescan that section when changes are made.  
> ##&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; only used if TriggerRescan is True;  
> ##&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - TriggerRescan - automatically trigger a deep rescan of changed sections. True or False.  
> ##  
> ##&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - KeepMode (Blank = Disabled | W = "Watched" | A = "Added")  
> ##&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method used to keep items for X number of days (specified by DaysToKeep below.)  
> ##&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; You may choose to base the keep on the last "watched" or "added" date.  
> ##&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - DaysToKeep ("30" = Keep items for 30 days following the KeepMode selected)  
> ##  
> ####################################################################################  
> ####################################################################################  
>   
> PC = ""  
> Host = ""  
> Port = ""  
> SectionList = [3]  
> IgnoreSections = []  
> Delete = "1"  
> Move = "0"  
> Copy = "0"  
> Shows = []  
> OnDeck = ""  
> DestinationDir = ""  
> DestinationSection = ""  
> TriggerRescan = True  
> KeepMode = ""  
> DaysToKeep = 30  
> ####################################################################################  
> ##&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NO NEED TO EDIT BELOW THIS LINE &nbsp;  
> ####################################################################################  
> import os  
> import xml.dom.minidom  
> import platform  
> import re  
> import shutil  
> import datetime  
> import glob  
> try:  
> &nbsp;&nbsp;&nbsp; import urllib.request as urllib2  
> except:  
> &nbsp;&nbsp;&nbsp; import urllib2  
>   
>   
> ####################################################################################  
> ##&nbsp; Check On Deck  
> ####################################################################################  
> def CheckOnDeck( CheckDeckFile ):  
> &nbsp; InTheDeck = 0  
> &nbsp; for DeckVideoNode in deck.getElementsByTagName("Video"):  
> &nbsp;&nbsp;&nbsp; DeckMediaNode = DeckVideoNode.getElementsByTagName("Media")  
> &nbsp;&nbsp;&nbsp; for DeckMedia in DeckMediaNode:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DeckPartNode = DeckMedia.getElementsByTagName("Part")  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for DeckPart in DeckPartNode:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Deckfile = DeckPart.getAttribute("file")  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if CheckDeckFile==Deckfile:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; InTheDeck += 1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; InTheDeck += 0  
> &nbsp; return InTheDeck  
> &nbsp;  
> ####################################################################################  
> ##&nbsp; Check Shows And Delete If Configured  
> ####################################################################################  
> def CheckShows( CheckFile ):  
> &nbsp; global FileCount  
> &nbsp; global DeleteCount  
> &nbsp; global MoveCount  
> &nbsp; global CopyCount  
> &nbsp; global FlaggedCount  
> &nbsp; global OnDeckCount  
> &nbsp; global ShowsCount  
> &nbsp; global DestinationDir  
> &nbsp; FileCount += 1  
> &nbsp; CantDelete = 0  
> &nbsp; ShowFound = ""  
> &nbsp; changed = 0  
> &nbsp;  
> ##&nbsp; -- CHECK SHOWS -- &nbsp;  
> &nbsp; for Show in Shows:  
> &nbsp;&nbsp;&nbsp; Show = re.sub('[^A-Za-z0-9]+', '', Show).strip()  
> &nbsp;&nbsp;&nbsp; if Show=="":  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CantDelete = 0  
> &nbsp;&nbsp;&nbsp; else:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (' ' in Show) == True:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if all(str(Word) in CheckFile for Word in Show.split()):  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CantDelete += 1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ShowFound = "[" + Show + "]"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ShowsCount += 1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CantDelete += 0  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if Show in CheckFile:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CantDelete += 1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ShowFound = "[" + Show + "]"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ShowsCount += 1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CantDelete += 0  
>   
> ## -- Check OnDeck --  
> &nbsp; if OnDeck=="1":  
> &nbsp;&nbsp;&nbsp; IsOnDeck = CheckOnDeck(CheckFile);  
> &nbsp;&nbsp;&nbsp; if IsOnDeck==0:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CantDelete += 0  
> &nbsp;&nbsp;&nbsp; else:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CantDelete += 1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ShowFound = "[OnDeck]" + ShowFound  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OnDeckCount += 1  
>   
> ## -- Check if video older than days to keep [Viewed method] --  
> &nbsp; if KeepMode=="W":  
> &nbsp;&nbsp;&nbsp; if DaysToKeep \> DaysSinceVideoLastViewed:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CantDelete += 1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ShowsCount += 1  
> #&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ShowFound = " | Days Since Viewed:"&nbsp; + str(DaysSinceVideoLastViewed) +" |" + ShowFound  
> ## -- Check if video older than days to keep [Added method] --  
> &nbsp; if KeepMode=="A":  
> &nbsp;&nbsp;&nbsp; if DaysToKeep \> DaysSinceVideoAdded:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CantDelete += 1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ShowsCount += 1  
> #&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ShowFound = " | Days Since Added:"&nbsp; + str(DaysSinceVideoAdded) +" |" + ShowFound  
> &nbsp;&nbsp; &nbsp;  
> ## -- DELETE SHOWS --  
> &nbsp; if CantDelete == 0:  
> &nbsp;&nbsp;&nbsp; if Delete=="1":  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #print("\*\*[DELETED] " + CheckFile)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; similairFiles = os.path.splitext(file)[0] + "\*"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print("Looking for " + similairFiles)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for deleteFile in glob.glob(similairFiles):  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; os.remove(deleteFile)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print("\*\*[DELETED] " + deleteFile)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; changed = 1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DeleteCount += 1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; except Exception as e:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print ("error deleting file: %s" % e)  
> &nbsp;&nbsp;&nbsp; elif Move == "1":  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; os.utime(os.path.realpath(CheckFile), None)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; shutil.move(os.path.realpath(CheckFile), DestinationDir)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print("\*\*[MOVED] " + CheckFile)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; changed = 1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; except Exception as e:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print ("error moving file: %s" % e)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if os.path.islink(CheckFile):  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; os.unlink(CheckFile)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MoveCount += 1  
> &nbsp;&nbsp;&nbsp; elif Copy == "1":  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; shutil.copy(os.path.realpath(CheckFile), DestinationDir)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; changed = 1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; except Exception as e:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print ("error copying file: %s" % e)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print("\*\*[COPIED] " + CheckFile)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CopyCount += 1  
>   
> &nbsp;&nbsp;&nbsp; else:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print("\*\*[FLAGGED] " + CheckFile)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FlaggedCount += 1  
> &nbsp; else:  
> &nbsp;&nbsp;&nbsp; print("[KEEPING]")  
> &nbsp; return changed  
>   
>   
>   
> ####################################################################################  
> ##&nbsp; Checking URL  
> ####################################################################################  
> if Host=="":  
> &nbsp; Host="127.0.0.1"  
> if Port=="":  
> &nbsp; Port="32400"  
>   
>   
> print("----------------------------------------------------------------------------")  
> print("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Detected Settings")  
> print("----------------------------------------------------------------------------")  
> print("Host: " + Host)  
> print("Port: " + Port)  
> #print("Section: " + Section)  
> #print("URL: " + URL)  
> #print("OnDeck URL: " + OnDeckURL)  
>   
> if KeepMode=="W":  
> &nbsp; print("Keep Mode: Keeping for " + str(DaysToKeep) + " days following the date last viewed")  
> elif KeepMode=="A":  
> &nbsp; print("Keep Mode: Keeping viewed items that were added to the library less than " + str(DaysToKeep) + " days ago")  
> else:  
> &nbsp; print("Keep Mode: DISABLED")  
>   
> ####################################################################################  
> ##&nbsp; Checking Shows  
> ####################################################################################  
> NoDelete = " | "  
> ShowCount = len(Shows)  
> print("Show Count: " + str(ShowCount))  
>   
> for Show in Shows:  
> &nbsp; Show = re.sub('[^A-Za-z0-9]+', '', Show).strip()  
> &nbsp; if Show=="":  
> &nbsp;&nbsp;&nbsp; NoDelete += "(None Listed) | "  
> &nbsp;&nbsp;&nbsp; ShowCount -= 1  
> &nbsp; else:  
> &nbsp;&nbsp;&nbsp; NoDelete += Show + " | "  
>   
> print("Number of Shows Detected For Keeping: " + str(ShowCount))  
> print ("Shows to Keep:" + NoDelete)  
>   
> ###################################################################################  
> ##&nbsp; Checking Delete  
> ####################################################################################  
> if Delete=="1":  
> &nbsp; print("Delete: \*\*\*Enabled\*\*\*")  
> else:  
> &nbsp; print("Delete: Disabled - Flagging Only")  
>   
> if OnDeck=="1":  
> &nbsp; print("Delete OnDeck: No")  
> else:  
> &nbsp; print("Delete OnDeck: Yes")  
>   
> ####################################################################################  
> ##&nbsp; Checking OS  
> ####################################################################################  
> AD = ""  
> if PC=="":  
> &nbsp; AD = "(Auto Detected)"  
> &nbsp; if platform.system()=="Windows":  
> &nbsp;&nbsp;&nbsp; PC = "W"  
> &nbsp; elif platform.system()=="Linux":  
> &nbsp;&nbsp;&nbsp; PC = "L"  
> &nbsp; elif platform.system()=="Darwin":  
> &nbsp;&nbsp;&nbsp; PC = "L"  
>   
> FileCount = 0  
> DeleteCount = 0  
> MoveCount = 0  
> CopyCount = 0  
> FlaggedCount = 0  
> OnDeckCount = 0  
> ShowsCount = 0  
> RescannedSections = []  
>   
>   
> ####################################################################################  
> ##&nbsp; Get list of sections  
> ####################################################################################  
>   
> if PC=="L":  
> &nbsp; print("Operating System: Linux " + AD)  
> &nbsp; import urllib2  
> elif PC=="W":  
> &nbsp; print("Operating System: Windows " + AD)  
> &nbsp; import urllib.request  
> else:  
> &nbsp; print("Operating System: \*\* Not Configured \*\*&nbsp; (" + platform.system() + ") is not recognized.")  
> &nbsp; exit()  
>   
> if not SectionList:  
> &nbsp; URL = ("http://" + Host + ":" + Port + "/library/sections/")  
> &nbsp; doc = xml.dom.minidom.parse(urllib2.urlopen(URL))  
> &nbsp; for Section in doc.getElementsByTagName("Directory"):  
> &nbsp;&nbsp;&nbsp; if Section.getAttribute("key") not in IgnoreSections:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SectionList.append(Section.getAttribute("key"))  
>   
> &nbsp; SectionList.sort(key=int)  
> &nbsp; print ("Section List Mode: Auto")  
> &nbsp; print ("Operating on sections: "&nbsp; +&nbsp; ','.join(str(x) for x in SectionList))  
> &nbsp; print ("Skipping Sections: " +&nbsp;&nbsp; ','.join(str(x) for x in IgnoreSections))  
>   
> else:  
> &nbsp; print ("Section List Mode: User-defined")  
> &nbsp; print ("Operating on user-defined sections: "&nbsp; +&nbsp; ','.join(str(x) for x in SectionList))  
>   
>   
> ####################################################################################  
> ## Loop on sections  
> ####################################################################################  
> rescan\_destination = False  
>   
> for Section in SectionList:  
> &nbsp; Section = str(Section)  
> &nbsp; URL = ("http://" + Host + ":" + Port + "/library/sections/" + Section + "/recentlyViewed")  
> &nbsp; OnDeckURL = ("http://" + Host + ":" + Port + "/library/sections/" + Section + "/onDeck")  
> &nbsp; ####################################################################################  
> &nbsp; ##&nbsp; Setting OS Based Variables  
> &nbsp; ####################################################################################  
> &nbsp; if PC=="L":  
> &nbsp;&nbsp;&nbsp; doc = xml.dom.minidom.parse(urllib2.urlopen(URL))  
> &nbsp;&nbsp;&nbsp; deck = xml.dom.minidom.parse(urllib2.urlopen(OnDeckURL))  
> &nbsp; elif PC=="W":  
> &nbsp;&nbsp;&nbsp; doc = xml.dom.minidom.parse(urllib.request.urlopen(URL))  
> &nbsp;&nbsp;&nbsp; deck = xml.dom.minidom.parse(urllib.request.urlopen(OnDeckURL))  
> &nbsp; SectionName = doc.getElementsByTagName("MediaContainer")[0].getAttribute("title1")  
> &nbsp; print("")  
> &nbsp; print("--------- Section "+ Section +": " + SectionName + " -----------------------------------")  
>   
>   
>   
> &nbsp; ####################################################################################  
> &nbsp; ##&nbsp; Get Files for Watched Shows  
> &nbsp; ####################################################################################  
> &nbsp; changed = 0  
> &nbsp; for VideoNode in doc.getElementsByTagName("Video"):  
> &nbsp;&nbsp;&nbsp; view = VideoNode.getAttribute("viewCount")  
> &nbsp;&nbsp;&nbsp; if view == '':  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; view = 0  
> &nbsp;&nbsp;&nbsp; view = int(view)  
> &nbsp;&nbsp;&nbsp; ################################################################  
> &nbsp;&nbsp;&nbsp; ###Find number of days between date video was viewed and today  
> &nbsp;&nbsp;&nbsp; lastViewedAt&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = VideoNode.getAttribute("lastViewedAt")  
> &nbsp;&nbsp;&nbsp; d1 = datetime.datetime.today()  
> &nbsp;&nbsp;&nbsp; d2 = datetime.datetime.fromtimestamp(float(lastViewedAt))  
> &nbsp;&nbsp;&nbsp; DaysSinceVideoLastViewed = (d1 -&nbsp;&nbsp; d2).days  
> &nbsp;&nbsp;&nbsp; ################################################################  
> &nbsp;&nbsp;&nbsp; ################################################################  
> &nbsp;&nbsp;&nbsp; ###Find number of days between date video was added and today  
> &nbsp;&nbsp;&nbsp; addedAt&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = VideoNode.getAttribute("addedAt")  
> &nbsp;&nbsp;&nbsp; d1 = datetime.datetime.today()  
> &nbsp;&nbsp;&nbsp; da2 = datetime.datetime.fromtimestamp(float(addedAt))  
> &nbsp;&nbsp;&nbsp; DaysSinceVideoAdded = (d1 -&nbsp;&nbsp; da2).days  
> &nbsp;&nbsp;&nbsp; ################################################################  
> &nbsp;&nbsp;&nbsp; MediaNode = VideoNode.getElementsByTagName("Media")  
> &nbsp;&nbsp;&nbsp; for Media in MediaNode:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PartNode = Media.getElementsByTagName("Part")  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for Part in PartNode:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; file = Part.getAttribute("file")  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #NEW (Removed by Mustang):  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #if PC == 'L':  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #file = urllib2.unquote(file.encode('utf-8')).decode('utf-8')  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #else:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #import urllib  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #file = urllib.parse.unquote(file.encode('utf-8')).decode('utf-8')  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if str(view)!="0":  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print(" ")  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if KeepMode=="W":  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if str(view)!="0":  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print ("Viewed:" + str(view) + "x | Days Since Viewed: " + str(DaysSinceVideoLastViewed) + " | " + file)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if view \> 0:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if os.path.isfile(file):  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; changed += CheckShows(file);  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print("##[NOT FOUND] " + file)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if KeepMode=="A":  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if str(view)!="0":  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print ("Viewed:" + str(view) + "x | Days Since Added: " + str(DaysSinceVideoAdded) + " | " + file)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if view \> 0:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if os.path.isfile(file):  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; changed += CheckShows(file);  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print("##[NOT FOUND] " + file)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if KeepMode=="":  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if str(view)!="0":  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print ("Viewed:" + str(view) + "x | Days Since Viewed: " + str(DaysSinceVideoLastViewed) + " Added: " + str(DaysSinceVideoAdded) + " | " + file)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if view \> 0:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if os.path.isfile(file):  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; changed += CheckShows(file);  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print("##[NOT FOUND] " + file)  
>   
> &nbsp; # rescan this section if necessary  
> &nbsp; if TriggerRescan and changed:  
> &nbsp;&nbsp;&nbsp; URL = ("http://" + Host + ":" + Port + "/library/sections/" + Section + "/refresh?deep=1")  
> &nbsp;&nbsp;&nbsp; blah = urllib2.urlopen(URL)  
> &nbsp;&nbsp;&nbsp; RescannedSections.append(SectionName + '(' + Section + ')')  
> &nbsp;&nbsp;&nbsp; rescan\_destination = True  
>   
> # rescan destination section if necessary  
> if TriggerRescan and DestinationSection and rescan\_destination:  
> &nbsp; URL = ("http://" + Host + ":" + Port + "/library/sections/" + str(DestinationSection) + "/refresh?deep=1")  
> &nbsp; blah = urllib2.urlopen(URL)  
> &nbsp; RescannedSections.append('[destination dir]' + '(' + str(DestinationSection) + ')')  
> &nbsp;&nbsp; &nbsp;  
>   
> print("")  
> print("----------------------------------------------------------------------------")  
> print("----------------------------------------------------------------------------")  
> print("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Summary -- Script Completed Successfully")  
> print("----------------------------------------------------------------------------")  
> print("")  
> print("&nbsp; Total File Count&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " + str(FileCount))  
> print("&nbsp; Kept Show Files&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " + str(ShowsCount))  
> print("&nbsp; On Deck Files&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " + str(OnDeckCount))  
> print("&nbsp; Deleted Files&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " + str(DeleteCount))  
> print("&nbsp; Moved Files&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " + str(MoveCount))  
> print("&nbsp; Copied Files&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " + str(CopyCount))  
> print("&nbsp; Flagged Files&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " + str(FlaggedCount))  
> print("&nbsp; Rescanned Sections&nbsp;&nbsp;&nbsp; " +&nbsp; ', '.join(str(x) for x in RescannedSections) )  
> print("")  
> print("----------------------------------------------------------------------------")  
> print("----------------------------------------------------------------------------")

---

<div class="post-metadata">

**Author:** ![eskwire](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/eskwire/32/3842_2.png) [@eskwire](https://forums.plex.tv/u/eskwire)\
**Post date:** [July 24, 2014, 3:27am UTC](https://forums.plex.tv/t/plex-auto-delete-script/45826/88 "2014-07-24T03:27:31Z")

</div>

Works perfect for me thanks a lot @mordamus.

Would be so cool if they include this in the PHT or PMS in a advance mode. like use under your own risk.

---

<div class="post-metadata">

**Author:** ![brotbuexe](https://avatars.discourse-cdn.com/v4/letter/b/6f9a4e/32.png) [@brotbuexe](https://forums.plex.tv/u/brotbuexe)\
**Post date:** [July 25, 2014, 5:08am UTC](https://forums.plex.tv/t/plex-auto-delete-script/45826/89 "2014-07-25T05:08:25Z")

</div>

Same for me &nbsp;with changed script from @mordamus.

Thanks guys.

---

<div class="post-metadata">

**Author:** ![joshandtara](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/joshandtara/32/3876_2.png) [@joshandtara](https://forums.plex.tv/u/joshandtara)\
**Post date:** [July 25, 2014, 11:59am UTC](https://forums.plex.tv/t/plex-auto-delete-script/45826/90 "2014-07-25T11:59:32Z")

</div>

Just come across this thread and am wondering how difficult it would be to get this script to delete high-quality watched episodes? I tend to download both SD and 720 copies, but i like to delete the 720 ones after i watch them and keep the HD ones.

---

<div class="post-metadata">

**Author:** ![brotbuexe](https://avatars.discourse-cdn.com/v4/letter/b/6f9a4e/32.png) [@brotbuexe](https://forums.plex.tv/u/brotbuexe)\
**Post date:** [July 29, 2014, 3:42am UTC](https://forums.plex.tv/t/plex-auto-delete-script/45826/91 "2014-07-29T03:42:15Z")

</div>

@mordamus, looks like there is an encoding issue:

Looking for D:\vids v\Switched at Birth\Season 3\Switched at Birth - S03E09 - The Past (Forgotten-Swallowed) [WEBDL-1080p]\*

Traceback (most recent call last):

&nbsp; File "c:\TOOLS\autodel.py", line 387, in 

&nbsp; &nbsp; changed += CheckShows(file);

&nbsp; File "c:\TOOLS\autodel.py", line 158, in CheckShows

&nbsp; &nbsp; for deleteFile in glob.glob(similairFiles):

&nbsp; File "C:\Python33\lib\glob.py", line 18, in glob

&nbsp; &nbsp; return list(iglob(pathname))

&nbsp; File "C:\Python33\lib\glob.py", line 50, in iglob

&nbsp; &nbsp; for name in glob\_in\_dir(dirname, basename):

&nbsp; File "C:\Python33\lib\glob.py", line 69, in glob1

&nbsp; &nbsp; return fnmatch.filter(names, pattern)

&nbsp; File "C:\Python33\lib\fnmatch.py", line 52, in filter

&nbsp; &nbsp; match = \_compile\_pattern(pat)

&nbsp; File "C:\Python33\lib\functools.py", line 275, in wrapper

&nbsp; &nbsp; result = user\_function(\*args, \*\*kwds)

&nbsp; File "C:\Python33\lib\fnmatch.py", line 46, in \_compile\_pattern

&nbsp; &nbsp; return re.compile(res).match

&nbsp; File "C:\Python33\lib\re.py", line 214, in compile

&nbsp; &nbsp; return \_compile(pattern, flags)

&nbsp; File "C:\Python33\lib\re.py", line 281, in \_compile

&nbsp; &nbsp; p = sre\_compile.compile(pattern, flags)

&nbsp; File "C:\Python33\lib\sre\_compile.py", line 494, in compile

&nbsp; &nbsp; p = sre\_parse.parse(p, flags)

&nbsp; File "C:\Python33\lib\sre\_parse.py", line 748, in parse

&nbsp; &nbsp; p = \_parse\_sub(source, pattern, 0)

&nbsp; File "C:\Python33\lib\sre\_parse.py", line 360, in \_parse\_sub

&nbsp; &nbsp; itemsappend(\_parse(source, state))

&nbsp; File "C:\Python33\lib\sre\_parse.py", line 506, in \_parse

&nbsp; &nbsp; raise error("bad character range")

sre\_constants.error: bad character range

&nbsp;

&nbsp;

I hope I fixed it myself by insert this at line 157

&nbsp;

&nbsp; &nbsp; &nbsp; # replace the left square bracket with [[]

&nbsp; &nbsp; &nbsp; similairFiles = re.sub(r'\[', '[[]', similairFiles)

&nbsp; &nbsp; &nbsp; # replace the right square bracket with []] but be careful not to replace

&nbsp; &nbsp; &nbsp; # the right square brackets in the left square bracket's 'escape' sequence.

&nbsp; &nbsp; &nbsp; similairFiles = re.sub(r'(?\<!\[)\]', '[]]', similairFiles)

&nbsp;

(from&nbsp;[http://edgylogic.com/blog/escaping-square-brackets-pythons-glob/)](http://edgylogic.com/blog/escaping-square-brackets-pythons-glob/))

---

<div class="post-metadata">

**Author:** ![woodsfehr](https://avatars.discourse-cdn.com/v4/letter/w/e9a140/32.png) [@woodsfehr](https://forums.plex.tv/u/woodsfehr)\
**Post date:** [September 12, 2014, 9:36pm UTC](https://forums.plex.tv/t/plex-auto-delete-script/45826/92 "2014-09-12T21:36:11Z")

</div>

This script is one more reason I'm glad I switched from XBMC to Plex. I was doing some auto cleaning in XBMC but it was with a bash script and finding old files.

I'm wondering if the option for Shows to keep (Shows = ["Frozen Planet"] could have an opposite option so instead of shows to keep, you could list the shows to delete.

Great script. Thanks

---

<div class="post-metadata">

**Author:** ![wheezer](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/wheezer/32/2654_2.png) [@wheezer](https://forums.plex.tv/u/wheezer)\
**Post date:** [September 17, 2014, 1:20am UTC](https://forums.plex.tv/t/plex-auto-delete-script/45826/93 "2014-09-17T01:20:51Z")

</div>

hey thanks for this, what an awesome script!

one suggestion: how about, instead of having a variable in script that determines what not to delete "Shows", we rely on checking whether the file in question is part of a collection in the DB. If it is, then do not delete. This would be a lot more elegant than having to maintain a list within the script!

---

<div class="post-metadata">

**Author:** ![eskwire](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/eskwire/32/3842_2.png) [@eskwire](https://forums.plex.tv/u/eskwire)\
**Post date:** [September 25, 2014, 2:04am UTC](https://forums.plex.tv/t/plex-auto-delete-script/45826/94 "2014-09-25T02:04:29Z")

</div>

I create a New Section name Anime when i run the doce he dont delete de show in Anime in stead he came back and say:

Viewed:1x | Days Since Viewed: 12 Added: 13 | /Users/eskwire/Downloads/focker/Anime/Sword Art Online/Season 3/[HorribleSubs] Sword Art Online II - 07 [720p].mkv

Looking for /Users/eskwire/Downloads/focker/Anime/Sword Art Online/Season 3/[HorribleSubs] Sword Art Online II - 07 [720p]\*

&nbsp;

&nbsp;

&nbsp;

it seem he is looking for the file and the file is listed on PMS and the section to scan i have it configure for [] no number. help advice are welcome.

---

<div class="post-metadata">

**Author:** ![woodsfehr](https://avatars.discourse-cdn.com/v4/letter/w/e9a140/32.png) [@woodsfehr](https://forums.plex.tv/u/woodsfehr)\
**Post date:** [September 25, 2014, 7:23pm UTC](https://forums.plex.tv/t/plex-auto-delete-script/45826/95 "2014-09-25T19:23:14Z")

</div>

I have a question about deleting and using shared libraries. If I share my television library and one of my guests watches an episode, will it flag it for deletion? I assume it is only checking my account for viewed/unviewed status. Is that correct? I'd hate to fall behind on a show that my guests have watched and find it has been deleted when I go to watch it.

---

<div class="post-metadata">

**Author:** ![wheezer](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/wheezer/32/2654_2.png) [@wheezer](https://forums.plex.tv/u/wheezer)\
**Post date:** [September 25, 2014, 7:33pm UTC](https://forums.plex.tv/t/plex-auto-delete-script/45826/96 "2014-09-25T19:33:09Z")

</div>

Scottwf,

From what i can see, this is not catered for in the script - largely because the data source it is querying doesn't show shared views. A different datasource would need to be queried and the script would need to be changed. I would welcome that change, but I think that it almost constitutes a rewrite.

---

<div class="post-metadata">

**Author:** ![woodsfehr](https://avatars.discourse-cdn.com/v4/letter/w/e9a140/32.png) [@woodsfehr](https://forums.plex.tv/u/woodsfehr)\
**Post date:** [September 25, 2014, 7:35pm UTC](https://forums.plex.tv/t/plex-auto-delete-script/45826/97 "2014-09-25T19:35:09Z")

</div>

So if I understand you correctly, a shared user watching an episode won't cause it to be flagged for deletion, right? I just wanted to make sure because I don't want a shared user causing my shows to be deleted.

Thanks

---

<div class="post-metadata">

**Author:** ![wheezer](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/wheezer/32/2654_2.png) [@wheezer](https://forums.plex.tv/u/wheezer)\
**Post date:** [September 25, 2014, 8:20pm UTC](https://forums.plex.tv/t/plex-auto-delete-script/45826/98 "2014-09-25T20:20:18Z")

</div>

scottwf - yes, that is correct

---

<div class="post-metadata">

**Author:** ![facted](https://avatars.discourse-cdn.com/v4/letter/f/4bbf92/32.png) [@facted](https://forums.plex.tv/u/facted)\
**Post date:** [October 9, 2014, 12:22am UTC](https://forums.plex.tv/t/plex-auto-delete-script/45826/99 "2014-10-09T00:22:12Z")

</div>

Is there anyway to ignore a certain directory of files? My issue is I want to move my watched files to a second directory, but I want to keep that second directory in the initial library so that my "OnDeck" functionality remains. I need to move my watched files so I can sync accurately with a tablet.

Also, my shows are organized into folders by show name. When I move the show using this script, is there anyway to maintain that folder structure?

---

<div class="post-metadata">

**Author:** ![eskwire](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/eskwire/32/3842_2.png) [@eskwire](https://forums.plex.tv/u/eskwire)\
**Post date:** [October 24, 2014, 4:20pm UTC](https://forums.plex.tv/t/plex-auto-delete-script/45826/100 "2014-10-24T16:20:13Z")

</div>

Am having a problem, my server have 3 section, 1 Movies, 2 TV Show, 3 TV Show(anime) but when i run the scrip it never run it on the section 3. any advice what should i do?

know i get this error please help:

/Downloads/focker/Anime/Hajime no Ippo/Season 1/[Utsukushii-Raws] Hajime no Ippo - The Fighting - 07 (DVD 720x480 H264 AC3 2.0 Chap Sub).mkv

Looking for /Users/hectorhenry/Downloads/focker/Anime/Hajime no Ippo/Season 1/[Utsukushii-Raws] Hajime no Ippo - The Fighting - 07 (DVD 720x480 H264 AC3 2.0 Chap Sub)\*

Traceback (most recent call last):

&nbsp; File "AutoDelete.py", line 387, in

&nbsp; &nbsp; changed += CheckShows(file);

&nbsp; File "AutoDelete.py", line 158, in CheckShows

&nbsp; &nbsp; for deleteFile in glob.glob(similairFiles):

&nbsp; File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/glob.py", line 27, in glob

&nbsp; &nbsp; return list(iglob(pathname))

&nbsp; File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/glob.py", line 59, in iglob

&nbsp; &nbsp; for name in glob\_in\_dir(dirname, basename):

&nbsp; File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/glob.py", line 78, in glob1

&nbsp; &nbsp; return fnmatch.filter(names, pattern)

&nbsp; File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/fnmatch.py", line 54, in filter

&nbsp; &nbsp; \_cache[pat] = re.compile(res)

&nbsp; File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.py", line 190, in compile

&nbsp; &nbsp; return \_compile(pattern, flags)

&nbsp; File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.py", line 242, in \_compile

&nbsp; &nbsp; raise error, v # invalid expression

sre\_constants.error: bad character range

[Previous page](https://forums.plex.tv/t/plex-auto-delete-script/45826.md?page=4)

[Next page](https://forums.plex.tv/t/plex-auto-delete-script/45826.md?page=6)
