Repair a Corrupt Plex Database

it is possible for your main Plex Media Server database to become corrupted. For instance, it might happen if the computer is turned off without first quitting Plex Media Server. In such a situation, you can attempt to repair the database.

Tip!: It’s always a good idea make a backup copy of the database file before doing any work on it.

First we need to stop the Plex Media Server and locate the database

sudo service plexmediaserver stop
Database location (ubuntu)
/var/lib/plexmediaserver/Library/Application Support/Plex Media Server/Plug-in Support/Databases

Now that plex is stopped we can check the database. This does require you have sqlite3 installed and ready to use. (Place holder to sqlite3 setup).

Check for Corruption

You can run commands to check how corrupt the database might be. Even if the results come back “OK”, it’s still possible for there to be issues with a database, though.

sudo cp com.plexapp.plugins.library.db com.plexapp.plugins.library.db.original

sudo sqlite3 com.plexapp.plugins.library.db "DROP index 'index_title_sort_naturalsort'"

sudo sqlite3 com.plexapp.plugins.library.db "DELETE from schema_migrations where version='20180501000000'"

sudo sqlite3 com.plexapp.plugins.library.db "PRAGMA integrity_check"

Run Database Repair

Even if the integrity check doesn’t return errors, it can still sometimes help to try and repair the database. In order to try and “repair” the database, you can perform a dump followed by a restore to get it back to normal. Run the following commands in the Command Prompt in order. 
sudo mv com.plexapp.plugins.library.db com.plexapp.plugins.library.db.original

sudo sqlite3 com.plexapp.plugins.library.db "DROP index 'index_title_sort_naturalsort'"

sudo sqlite3 com.plexapp.plugins.library.db "DELETE from schema_migrations where version='20180501000000'"

sudo sqlite3 com.plexapp.plugins.library.db .dump > dump.sql

sudo rm com.plexapp.plugins.library.db

sudo sqlite3 com.plexapp.plugins.library.db < dump.sql

Prior to restarting your Plex Media Server, you should also delete (or backup and move elsewhere) the following two files if they exist alongside the original database:

com.plexapp.plugins.library.db-shm
com.plexapp.plugins.library.db-wal

Start Plex and Check

sudo service plexmediaserver start

Now the plex database has been repaired and can be tested. In some cases, the repair process does not fix the problem and a database restore might be required to restore plex.

Stop plex again using the steps above and move to the last resort to restore the backup

Last Resort – Restore Plex Database from backup

One of the many Scheduled Tasks that a Plex Media Server performs is to make regular backups of the core database, which holds things such as your viewstate information and media matching details. Below are the steps to restore from one of those backups

sudo mv com.plexapp.plugins.library.db com.plexapp.plugins.library.db-ARBackup

ls (list all database backups)
com.plexapp.plugins.library.db-2019-04-19
com.plexapp.plugins.library.db-2019-04-22
com.plexapp.plugins.library.db-2019-04-25

sudo cp com.plexapp.plugins.library.db-2019-04-25 com.plexapp.plugins.library.db

sudo rm com.plexapp.plugins.library.db-shm
sudo rm com.plexapp.plugins.library.db-wal

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.