How I Automatically Empty Trash When Files are Deleted

After reading the title you may be wondering why I want to have Plex automatically empty the trash if I delete a file. You may also be wondering in what scenario I find myself having to delete files regularly to the point where I need to automate the process.

I recently realized such a scenario that I will explain later in this post. Perhaps the scenario could be something you may encounter as you manage your Plex server.

First I will explain the process I use to have Plex automatically empty the trash when I delete a media file I no longer need.

How I Automatically Empty Trash When Files are Deleted in Plex

How do I get Plex to automatically empty trash on file deletion?

When I delete a media file from my file system I use the Plex API to have Plex scan the changed file and then empty the trash. This allows me some control over when Plex empties the trash as well as when it does so.

To automate this process, I use an open-source tool called FileWatcher.

FileWatcher is simply a file and folder monitor that runs on my Windows server as a service. It monitors changes to folders that I specify in the config file, and when a change is detected, it performs specific actions.

FileWatcher is the same application I use to automatically scan a library when a file has been added or changed.

In this case, I use FileWatcher to monitor for file deletions, and when a deletion is detected, send two API requests to my Plex server - one to perform the partial library scan, and the second to empty the trash.

Configuring FileWatcher

Since FileWatcher is the application that will send the Plex API requests on file deletion, it is important to configure the application.

Below is the XML configuration I use for automatically removing a media file from Plex when I delete the file. An explanation of the settings is below.

<watches>
    <watch>
        <path>I:\Pictures</path>
        <notifications>
            <waittime>30000</waittime>
            <notification>
                <triggers>
                    <trigger>Create</trigger>
                    <trigger>Change</trigger>
                    <trigger>Delete</trigger>
                </triggers>
                <method>GET</method>
                <url>[env:plex_url]/library/sections/1/refresh?path=[urlenc:I:\Pictures\[path]]&amp;X-Plex-Token=[env:plex_token]</url>
            </notification>           
        </notifications>                  
    </watch>
    <watch>
        <path>I:\Pictures</path>
        <notifications>
            <waittime>37000</waittime>
            <notification>
                <triggers>
                    <trigger>Delete</trigger>
                </triggers>
                <method>PUT</method>
                <url>[env:plex_url]/library/sections/1/emptyTrash?X-Plex-Token=[env:plex_token]</url>
            </notification>            
        </notifications>
    </watch>
</watches>

The config file may seem complex, but there are two watches, one for performing the scan and the second to empty the trash. The scan API request is sent when a file is created, changed, or deleted. The trash is emptied only when a file is deleted.

For the scan request, the following information is provided in the configuration file:

path
This is the full path to my Plex media folder. In my example, it is the full path to my movies library folder.
notifications
This outlines that I would like to send a request to an endpoint. In this case, I will be sending a request to my Plex server to perform a partial scan.
waittime
A wait time of 30,000 milliseconds (30 seconds) is specified. FileWatcher queues API requests and sends them after a specified amount of time. In this case, the requests will be sent every 30 seconds.
triggers
This determines what changes will need to happen to send the request. The notifications will be sent out when a file is created, changed, or deleted.
method
This is the method used to send the request. The Plex API information indicates the Plex partial scan is sent using the GET method.
url
This is the URL to use for the Plex partial scan. This can be found in the Plex API documentation. The values starting with [env:<value>] indicate an environment variable. So the plex_url and plex_token are environment variables containing the Plex URL of my server, and the Plex token, respectively. The [urlenc:<value>] will URL-encode the value to send to the Plex server. The [path] placeholder will be replaced with the path of the changed movie item.

For the empty trash API request, the following information is provided:

waittime
The empty trash API needs to run after the scan request. The issue is that once a scan request is sent, a response is returned immediately, so there is no way of waiting for the scan to complete. To get around this issue, I specified the wait time for the empty trash request to be 37,000 milliseconds (37 seconds), which gives the scan 7 seconds to complete before the empty trash request is sent to the Plex server.
triggers
Since I only want the empty trash request to be sent on file deletion, I specify Delete for the trigger.
method
The empty trash API uses the PUT method.
url
This is the URL to use for the Plex empty trash request. This can be found in the Plex API documentation. The values starting with [env:<value>] indicate an environment variable. So the plex_url and plex_token are environment variables containing the Plex URL of my server, and the Plex token, respectively.

Here is a summary of what happens when a file is deleted:

  1. I delete a file from the server, which causes the FileWatcher application that is monitoring that folder for deletions to queue both the partial scan for the file and the empty trash requests.
  2. After 30 seconds, the partial scan request is sent to the Plex server to have Plex determine that a media file has been deleted.
  3. After another 7 seconds, the empty trash request is sent to the Plex server to have Plex clean up any information about the deleted files.

Why I don't have Plex automatically empty trash

As you may be aware, Plex has a setting that will automatically empty trash after each scan. By default, this setting is enabled to allow a Plex server to remove media information from its database that no longer exists.

In theory, this seems like a great way to keep the data on a Plex server clean. In practice, however, this setting can cause issues. The biggest problem is that sometimes the location of the media files can be unavailable to Plex. This can happen if Plex loses connection to a NAS containing the files, or a DAS that didn't restart properly.

When a situation such as that happens and Plex runs a scan, all the media files won't be found by Plex. With the empty trash automatically setting in Plex enabled, Plex will remove all information about those missing files, which could result in all media being removed from Plex.

To avoid this issue, I disabled the setting for Plex to empty trash after every scan and instead use the method I described above.

My scenario

Why would I need to automate the empty trash functionality of Plex? Since I don't use the built-in functionality, I do need a method that is automated, but on my schedule.

The reason for this is that I have automated the uploading of photos from the mobile devices in my household. When one of the mobile devices is connected to our home WiFi, it will automatically upload any new photos and videos to the server, which will then automatically get added to Plex.

The issue is one of my children ends up with a lot of unnecessary image files that get uploaded. These image files could be screenshots of games or memes that aren't family photos, and I don't want to take up space on my server and display them in Plex.

When I see these images I delete them from the server, but I would then need to go into Plex each time to empty the trash. The solution I provided above means that I can just delete the unneeded image files at any time and have Plex automatically clean up any information about those images.

Since I was already using FileWatcher for other tasks on my server, it was a good solution for me to implement in this scenario.

Photo of Paul Salmon
Started managing a Plex server in November 2014 and has been sharing his experience and what he has learned on Plexopedia. He is exploring and documenting the Plex API to help automate tasks for Plex to reduce the management effort of his server.
Subscribe
Display