diff --git a/MediaBrowser.Controller/Entities/Folder.cs b/MediaBrowser.Controller/Entities/Folder.cs index ecf99accb6..e6c94d3cbe 100644 --- a/MediaBrowser.Controller/Entities/Folder.cs +++ b/MediaBrowser.Controller/Entities/Folder.cs @@ -486,6 +486,9 @@ namespace MediaBrowser.Controller.Entities var itemsRemoved = currentChildren.Values.Except(validChildren).ToList(); var shouldRemove = !IsRoot || allowRemoveRoot; // If it's an AggregateFolder, don't remove + // Collect old primaries that need demotion to alternates of newly created primaries + var oldPrimariesToDemote = new List<(Video OldPrimary, Video NewPrimary)>(); + if (shouldRemove && itemsRemoved.Count > 0) { // Build a set of paths that are alternate versions of valid children @@ -517,6 +520,19 @@ namespace MediaBrowser.Controller.Entities // Check if path is in LocalAlternateVersions of any valid child if (!string.IsNullOrEmpty(item.Path) && alternateVersionPaths.Contains(item.Path)) { + // If this was a primary (no PrimaryVersionId, no OwnerId), it needs demotion + if (video.OwnerId.IsEmpty()) + { + var newPrimary = newItems + .OfType