Skip to content

🟠 Catalog: purge-virtual orphan cleanup deletes nothing — NOT IN vs NULL content_id #2

Closed

Description

@dreulavelle

Severity: 🟠 High · Area: Catalog / Admin · Type: Correctness (silently broken feature)
Branch: feat/virtual-playback-upstream @ 53866e6

Summary

The admin "purge virtual items" action deletes the aiostreams:// file rows but then deletes zero orphaned catalog items, because its orphan query uses NOT IN against a subquery that can contain NULL. Every purged title stays in the catalog as a permanent ghost entry with no files.

Where

internal/catalog/item_repo.go:838-842 (step 2 of PurgeVirtualPlaybackItems):

resItems, err := tx.Exec(ctx, `
	DELETE FROM media_items 
	WHERE content_id NOT IN (SELECT DISTINCT content_id FROM media_files)
`)

What goes wrong

media_files.content_id is nullable, and NULL rows exist in normal operation — the scanner's own unmatched query filters on mf.content_id IS NULL (file_repo.go:1959). In SQL, x NOT IN (subquery) evaluates to UNKNOWN (never true) for every row as soon as the subquery yields a single NULL. So whenever even one unmatched media file exists anywhere in the DB, step 2 deletes nothing, and step 3's collection-link cleanup consequently has nothing to clean. The endpoint reports success while leaving ghost items behind.

Suggested fix

Either filter NULLs inside the subquery:

WHERE content_id NOT IN (SELECT content_id FROM media_files WHERE content_id IS NOT NULL)

or (preferred) rewrite as NOT EXISTS, which has sane NULL semantics:

DELETE FROM media_items mi
WHERE NOT EXISTS (SELECT 1 FROM media_files mf WHERE mf.content_id = mi.content_id)

Filed from an automated multi-agent audit of this branch; finding verified against the code at the cited commit.

Activity

  1. drondeseries commented on Jul 23, 2026

    @drondeseries
    Collaborator

    Fixed in commit 40a2a2b: rewritten PurgeVirtualPlaybackItems query to use NOT EXISTS (SELECT 1 FROM media_files mf WHERE mf.content_id = mi.content_id) to properly delete orphaned items when NULL content_id rows exist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions