[HowTo] Plex database modification - Moving media the right/wrong way

Is this out-dated? I ran the scripts, and I can play moved media files in Plex, but when Plex scan it deletes the old entries and adds them anew.

In my case, I got a new hard drive and moved my .mp4 files to it (didn’t see I was supposed to copy and not move until now)

Scripts:

insert into section_locations 
select 19 as id, library_section_id, 'G:\Movies' as root_path, available, scanned_at, created_at, updated_at 
from section_locations 
where root_path = 'F:\Movies';

UPDATE metadata_items
SET guid= replace(guid, 'F:\Movies\', 'G:\Movies\')
where guid like '%F:\Movies\%.mp4';

UPDATE media_parts
SET file= replace(file, 'F:\Movies\', 'G:\Movies\')
where file like '%F:\Movies\%.mp4';

UPDATE media_streams
SET url= replace(url, 'F:\Movies\', 'G:\Movies\')
where media_item_id in (select media_item_id from media_parts where file like 'G:\Movies\%.mp4');
1 Like

Ah, media_items has a section_location_id too…I probably need to update that as well.

Yeah, that was the issue…it’s working now. Cheers!

Script:

insert into section_locations 
select (select seq from sqlite_sequence where name = 'section_locations') as id, library_section_id, 'G:\Movies' as root_path, available, scanned_at, created_at, updated_at 
from section_locations 
where root_path = 'F:\Movies';

update sqlite_sequence 
set seq = seq + 1 
where name = 'section_locations';

UPDATE metadata_items
SET guid= replace(guid, 'F:\Movies\', 'G:\Movies\')
where guid like 'F:\Movies\%.mp4';

UPDATE media_parts
SET file= replace(file, 'F:\Movies\', 'G:\Movies\')
where file like 'F:\Movies\%.mp4';

UPDATE media_streams
SET url= replace(url, 'F:\Movies\', 'G:\Movies\')
where media_item_id in (select media_item_id from media_parts where file like 'G:\Movies\%.mp4');

update media_items 
set section_location_id = (select id from section_locations where root_path = 'G:\Movies') 
where id in (select media_item_id from media_parts where file like 'G:\Movies\%.mp4');
3 Likes

Hi everybody,

if you want to move media to an other location and you want to keep your items in PMS library intact (with all of your manually edited metadata) follow these steps:

  1. create an empty new folder where you will migrate your media
  2. add that newly created folder to the library (after you’re done it will automatically scan that empty folder)
  3. move (do not copy!) media to that new folder
  4. scan
  5. done

the trick is that you can move around media files among those folders as long the folders are indexed in the library and you respect folders-subfolders structure.

i’m not sure why this method is not an official plex method, it works flawlessly.

17 Likes

Why is there a need to increase the seq number?

THANK YOU!! This was the ONLY helpful post I could find for relocating my library – and the instructions are still valid :slight_smile:

This worked great! Much better and quicker than Plex’s official method and all the custom metadata was maintained! Thanks!

thanks, using the methods above, I was able to go into the metadata section, find the creation and added dates and then I was able to back date a few movies that I did not want showing up on the main page as newish movies.

steps
Downloaded the SQL viewer in the post , shutdown the plex server, open the database, found the metadata section and movie names, found the added date, modified the added date, modified the creation date as well just to make sure some code somewhere did not object to one being before the other???. wrote the database, opened the server, checked in the browser and sure enough the movies jumped further back in time. Wha la. I can now go add all those old embarrassing movies with them showing up in library (sharing with parents) as just added.

Will wait a day or two before doing the rest to make sure the over night updates dont just move them right back. :slight_smile:

Thank you @DavidIrwin that totally worked for moving one of my libraries to a new different drive and keeping the ‘watched’ data for my managed users! So glad i landed on this before i tried the Plex Support ‘mode media content to a new location’. Thank you so much, manually putting each file as ‘watched’ would’ve been a real pain!

1 Like

many thanks,
using SQLite database browser, I was able to move my library with the metadata intact
using these 3 scripts:

UPDATE section_locations
SET root_path= replace(root_path, ‘G:\videos’, ‘I:\videos’)
where root_path like ‘G:\videos%’;

UPDATE media_parts
SET file= replace(file, ‘G:\videos’, ‘I:\videos’)
where file like ‘G:\videos%’;

UPDATE media_streams
SET url= replace(url, ‘G:\videos’, ‘I:\videos’)
where url like ‘%G:\videos%’;

1 Like

I tried the commands at https://support.plex.tv/articles/201154527-move-viewstate-ratings-from-one-install-to-another/ to export my metadata for a move from Windows to Linux. Ended stuck with " …>" I scanned the posts above and it’s all over my head. I wish Plex would just Make an Import/Export command. (and import standard music playlists, and …)

A program designed to make media easy to organize and play should not be so hard to use.

3 Likes

I can now confirm the steps outlined by @levander in post number 43 about adding the empty directory to the library that you are planning to move your media to first, then moving (not copy) the files to the new directory then scanning the library… Worked like a charm and easier than editing the database… Thank you!!!

I had a similar need. I wanted to merge libraries. The actual location of the media was staying the same. I used sqlite browser. I looked at the table ‘library_locations’ to find Library B had an id of 21. I wanted to move a folder ‘/mnt/tv/animated’ to Library A which had an id 2.

What it was:

Library A (library_section_id = 2)
/mnt/tv/normal

Library B (library_section_id = 21)
/mnt/tv/animated

What I wanted:

Library A (library_section_id = 2)
/mnt/tv/normal
/mnt/tv/animated

Library B (library_section_id = 21)

Stopped plex. Backed up the db file. Updated the following tables. Started plex. All the metadata and watched state was still correct.

UPDATE section_locations
SET library_section_id = 2
WHERE
library_section_id = 21

UPDATE directories
SET library_section_id = 2
WHERE
library_section_id = 21

UPDATE media_items
SET library_section_id = 2
WHERE
library_section_id = 21


UPDATE metadata_item_views
SET library_section_id = 2
WHERE
library_section_id = 21

UPDATE metadata_items
SET library_section_id = 2
WHERE
library_section_id = 21
2 Likes

Any Update on this?

Update for what?

Do the steps in post #43 only work if you keep the original directory in the library? In my case I’ll be changing from two old external drives to one new external drive, and removing those external drives from my system. Would this method still work for me?

All three drives will have to be attached to start and as long as your operating system doesn’t change the path to your external drives when you remove the old ones… If it does, it doesn’t make it imposable but much more difficult…

Once you are done with the steps outlined you can remove the old, now empty original directory, from your Library. Then you may delete or remove the directory or drive from your system…

Fantastic! My OS doesn’t change the path (I assume you mean the drive letter) so it seems like I should be good to go?

Should be… It worked for me when I added another drive…

Awesome. Thanks so much!