Skip to content
TheAmigo edited this page Feb 14, 2022 · 4 revisions

Table of Contents

Intro

Easily automate the cleanup of old files in select Plex libraries.

Why would you delete content?

If you have an IP camera that saves videos to your Plex server, you probably don't want it to write continuously until the drive is full.

Why not just use cron and find to delete old files?

If you're using Plex to watch recordings, it's very convenient to use the Plex client to rate the videos with stars. Plex also remembers which ones you've watched. Both of those are factors this tool can use when deciding which files to delete.

Requirements

So far, it's only been tested on Linux, running on the same machine as the Plex Media Server.

It uses a couple Perl modules, of note is AnyEvent::HTTP, which isn't packaged for ArchLinux, but can be installed from CPAN:

sudo cpan
install AnyEvent::HTTP

Installation

  1. Download plex-cleanup.pl
  2. Put it somewhere in your path, perhaps:
    sudo mv plex-cleanup.pl /usr/local/bin/
  3. Make it executable:
    sudo chmod 755 /usr/local/bin/plex-cleanup.pl
  4. Add it to cron as a user that has permission to delete files from the libraries you want cleaned:
    crontab -e

Usage

When run from a terminal, it runs in test mode (only reporting what it would do, not deleting anything). When run from cron, it runs in live mode, cleaning up according to the configuration.

It detects terminal mode by checking if STDIN is a tty. So if you want to run in live mode from a terminal, just add </dev/null to the command line.

There are no required arguments, but without a config file, there's nothing for it to do.

By default, it prints out summary information on the cleanup of each library. Adding add -v, it will print out each file name as it deletes them. Adding a second -v, it will also print out the files it's not deleting and some metadata about each.

Configuration

A configuration file is required so it knows what libraries you want it to cleanup. The default location is $HOME/.config/plex-cleanup.conf, but that can be overridden on the command line with the -c argument.

The config file is INI-style. With global settings at the top and a section for each library that contains library-specific directives. A sample config file is included.

General config note:

  • It's quite flexible about whitespace in the file.
  • Boolean config settings may be specified as: y, yes, t, true, on, or 1 (case insensitively) for True. All other values are considered False.

Global config options

All global config settings are optional.

Name Default Description
plexhost localhost Hostname or IP address where Plex Media Server is running.
plexport 32400 Port name or number on which Plex Media Server is listening.

Per-library config options

Library sections must be named the same as the library to which they apply (and are case sensitive). Each section must have exactly one of the mode directives: age, size, or count.

Name Default Description
age   Delete all files in this library older than this.
size   Delete old files from this library until the size of the library is less than this size.
count   Delete old files from this library until there are no more than this many left.
watched_only False Only delete files that Plex has marked as watched.
continue_on_error False If a file can't be deleted, keep trying to delete more files from this library.

age

The default units for age is in seconds. You may append a suffix of: seconds, minutes, hours, or days (singular or plural, or just the first letter).

You cannot give multiple units ("1d 12h" doesn't work), but "1.5 days" does.

size

The default units for size is in bytes. You may append a single-letter suffix indicating an SI unit: K, M, G, T, P, E, Z, Y (in case you have a media library larger than the sum of all current digital storage). Where 1K == 1024 bytes, etc.

When cleaning up by size or count, files are ordered by Plex rating (stars you've given), then by age. If you never click on a rating, that's the same as setting your rating to zero stars. So the first files to be deleted will be the oldest of the videos with zero stars.

continue_on_error

Since a Plex library can comprise multiple directories, it's possible that the user running this script has permission to delete some files in a library and not others. This may even happen by mistake. If it happens that the older files can't be deleted but the younger ones can, this may result in undesirable behavior. So by default, if a file can't be deleted (e.g. permission denied or read only filesystem), no further deletes will be attempted in that library.

Since it's also possible that you configured such a scenario intentionally (e.g. moving select files into a root-owned subdirectory to keep them), then you may really want to delete other files. In this case, add continue_on_error = True to the configuration section for that library.

Sample config

A common, simple config might be:

 [Front camera]
 size = 10G

That will connect to the Plex server on localhost:32400 and go through the library named Front camera and delete files until there are no more than 10 GiB left. It will start with the files that have a zero star rating and delete the oldest of those first. If it can't delete one, it will stop cleaning up that library and exit (as there aren't any other libraries listed in the config).