Skip to content

How to automatically delete playlists

croneter edited this page Dec 23, 2018 · 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.

Get all the info you need to delete the playlists

  1. Exit EVERY Kodi instance that has PKC up and running
  2. For every Kodi instance with PKC, delete 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. 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
  5. 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 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.

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...')