Skip to content

How to automatically delete playlists

croneter edited this page Apr 20, 2019 · 19 revisions

How to delete all playlists from both Plex and Kodi

If you've been testing PKC thoroughly, it's possible that you unintentionally put PKC in a state where it kept spawning playlists on both the Plex and Kodi side. Using Plex Web, you'll see potentially hundreds to thousands of playlists listed, all of them ending in a digit like _15.

Delete all playlists on the Kodi side

  1. Exit EVERY Kodi instance that has PKC up and running
  2. For every Kodi instance with PKC, delete all subfolders and files except Addons27.db in the Database folder in the Kodi userdata folder. Also do this for all the Kodi profiles in the profiles folder, if you're using different Kodi users
  3. Also delete the playlists folder of the userdata folder and also do this for all profiles
  4. Do NOT restart Kodi until you're finished!

Delete all playlists on the Plex side

Easy first method to delete all playlists

This might or might not work for you! If you have to many playlists, please follow the difficult second method below!

  1. Using Plex Web, navigate to Playlists using the panel on the left side of the screen. You will see many, many playlists
  2. Mark/select the very first playlist. Scroll down to the last item, press the Shift-key and select the last playlist. All playlists will be selected. This might take a while!
  3. At the top right of the screen, hit the three dots ..., then Delete playlists

Difficult second method to delete all playlists

  1. Make sure that your Plex Media Server is NOT running!
  2. Get the PATH where Plex stores your server data, e.g. with the help of this Plex support article
    • Example: C:/users/username-i-need-to-replace/AppData/Local/Plex Media Server
  3. Run the Python 3 script below after you replaced the PATH variable with your own path from step 2 above.
    • If you don't know how to run a Python script, see e.g. this link
# -*- coding: utf-8 -*-
"""
Python 3 only!
WARNING: this script will delete all your existing Plex playlists!

Be sure that your Plex Media Server is NOT running when executing this script
"""
import sqlite3
import pathlib
import shutil

# Path to your Plex Media Server. Even for Windows, use forward slashes instead
# of backward slashes!
# Example:
# PATH = 'C:/users/username-i-need-to-replace/AppData/Local/Plex Media Server'
#
# Don't know where your Plex Media Server stores your data? See
# https://support.plex.tv/articles/202915258-where-is-the-plex-media-server-data-directory-located/
PATH = 'C:/users/username-i-need-to-replace/AppData/Local/Plex Media Server'


def main():
    path = pathlib.Path(PATH)
    if not path.exists():
        print('Path to Plex data directory not found!\n%s' % path)
        return
    original = path / 'Plug-in Support' / 'Databases' / 'com.plexapp.plugins.library.db'
    backup = path / 'Plug-in Support' / 'Databases' / 'com.plexapp.plugins.library.db.BACKUP'
    if not original.exists():
        print('com.plexapp.plugins.library.db not found!\n%s' % path)
        return
    print('Making a backup of the Plex database:\n%s' % backup)
    try:
        shutil.copyfile(original, backup)
    except (OSError, IOError) as error:
        print('Error encountered while creating a backup:')
        print(error)
        print('Do you have writing access to the Plex data directory?')
        return
    conn = sqlite3.connect(original, timeout=10.0)
    cursor = conn.cursor()
    # Need to drop an index first, just like when repairing the Plex DB
    cursor.execute("DROP index IF EXISTS 'index_title_sort_naturalsort'")
    # Let's also delete this, just like when repairing the Plex DB
    cursor.execute("DELETE FROM schema_migrations WHERE version='20180501000000'")
    # Actually deleting all playlists now - 15 represents Plex playlists
    cursor.execute('DELETE FROM metadata_items WHERE metadata_type = ?',
                   (15, ))
    conn.commit()
    conn.close()
    print('Done')


if __name__ == '__main__':
    main()

Difficult third method to delete all playlists

Get all the info you need to delete the playlists

  1. Using Plex Web, navigate to a single movie or show's information page. Hit the three dots ... at the top right, then Show Information, then Show XML-file. A new browser window opens
  2. The URL of the new browser window will be something like this:
https://82-123-96-251.7923ljfse892382.plex.direct:34698/library/metadata/12345?checkFiles=1&includeExtras=1&includeBandwidths=1&X-Plex-Token=alkjb84klafj83

For the script below, you'll need to copy-paste the following information:

  • PMS address: in the example above, https://82-123-96-251.7923ljfse892382.plex.direct
  • PMS port: in the example above, 34698
  • Your PMS token (don't share that with anyone!!): the string directly after the X-Plex-Token=, in the example above alkjb84klafj83
  1. Using Plex Web, navigate into the very first playlist that is listed. The browser window's URL will be something like this:
https://82-123-96-251.7923ljfse892382.plex.direct:34698/playlists/682194?<potentially-more-stuff>
  • Copy the playlist ID from the URL, in the example above 682194

Delete the playlists automatically

WARNING: All playlists with an ID higher than the one noted above (682194) will be deleted.

  • Run the following Python 2.7 (NOT Python 3.x!) script on any machine with network/internet access to your PMS. Both Python 2 and Python 3 will work. Replace the values with the ones you retrieved using the steps above.
  • Once you're satisfied with the results (check using Plex Web), restart Kodi and manually reset the Kodi database in the PKC settings under Advanced
import requests
import threading
import Queue
import time

# Copy-paste the details of your PMS here!
PMS_ADDRESS = 'https://82-123-96-251.7923ljfse892382.plex.direct'
PMS_PORT = 34698
PMS_TOKEN = 'alkjb84klafj83'

# Make sure that APPROXIMATE_NUMBER_OF_PLAYLISTS is high enough
FIRST_PLAYLIST_ID = 682194
APPROXIMATE_NUMBER_OF_PLAYLISTS = 500
# Advanced settings if you want more threads concurrently
NUMBER_OF_THREADS = 30


class MyThread(threading.Thread):
    def __init__(self, queue, args):
        self.queue = queue
        self.args = args
        threading.Thread.__init__(self)

    def run(self):
        while True:
            try:
                target = self.queue.get(block=False)
            except Queue.Empty:
                time.sleep(0.05)
            else:
                r = requests.delete(target, params=self.args)
                print(r.status_code)


URL = '%s:%s/playlists' % (PMS_ADDRESS, PMS_PORT)
URL += '/%s'
ARGS = {
    'X-Plex-Token': PMS_TOKEN
}
QUEUE = Queue.Queue()
for i in range(FIRST_PLAYLIST_ID,
               FIRST_PLAYLIST_ID + APPROXIMATE_NUMBER_OF_PLAYLISTS + 1):
    QUEUE.put(URL % i)

for i in range(0, NUMBER_OF_THREADS):
    t = MyThread(QUEUE, ARGS)
    t.daemon = True
    t.start()

print('Waiting to finish...')
QUEUE.join()
print('Done...')
Clone this wiki locally