588 ghost videos: what our family photo library taught me about cleanup code
My nightly video merger deleted the library entries it could remember. Everything from earlier runs survived as ghost thumbnails, 588 of them, until I stopped asking the job what it did and started asking the disk what exists.

There is one app in this house that everybody opens without asking: the photo library. My daughter scrolls it, my son finds his own birthday in it, and neither of them tells me when something is broken. They just stop opening it. So a bug in that library is not a technical detail. It is a door closing.
The nightly job I am proud of
Since 2019 we have filmed our life in short phone clips: hundreds of files scattered across folders nobody had time to open. My project was to turn each day into a single video, so that a whole afternoon can be watched at once instead of hunted through a dozen clips.
The merger runs every night at 1:00 on my Proxmox host, encoded with the GPU (H.265 through NVENC, 1080p). It finds the day's clips, joins them into one `Family Video YYYY-MM-DD.mp4`, and moves the source clips into a dated archive so the library stays tidy. Moving the originals is the whole point: one file per day, not thirty.
Moving a file, though, leaves a hole in a database that does not know files move.
Ghosts
Immich, the open source photo and video library I run in its own container, keeps a row for every asset with the path of the file behind it. When the merger moved a clip, its cleanup helper deleted the matching row. Exact path match, current run, done.
That worked, and it was still wrong. Anything the merger had moved in an earlier run, or in a batch that behaved slightly differently, kept its row: Live Photo companion `.mov` files, clips with UUID names, days that were re-merged into a second output. The rows stayed marked active, so the thumbnails kept rendering. Click one and nothing plays. The library had begun to lie about what it had.
I ran the reconciliation by hand on 2026-08-02. 588 ghost video rows. The library went from 46,514 assets to 45,926 the moment those rows went. Of the 588, 584 source files turned out to be safe in the archive; the other 4 were rows whose file had vanished entirely. Nothing was lost. Everything looked lost, which is a worse failure, because the family cannot tell the two apart.
The fix was a change of question
The old cleanup asked: *what did this run do?* A job's memory of its own past is exactly as reliable as the version of the job that ran back then, and that version has since changed.
The sweep asks: *what is actually on disk?* It runs inside the Immich container after every merge, so the nightly cron reconciles the database against the filesystem forever, no matter which version of the merger produced the mess.
The scope is what makes a delete safe, so it is narrow on purpose:
- only rows marked active; the Trash is never touched, - only the video extensions the merger handles (mp4, mov, mts, m2ts, avi, mkv, webm, 3gp, m4v), - only assets under the library root, so photos and uploaded items are out of reach, - and a row is deleted only when the file is confirmed absent, by `os.path.exists()`, in that same check.
The delete goes straight to PostgreSQL, and because the schema cascades, thumbnails, metadata and face crops go with the row instead of becoming ghosts of their own. There is a `--dry-run` flag for the day I stop trusting it, and the sweep always exits 0: a cleanup that fails must never take the merge down with it.
This morning's numbers
I did not want a fix I had to believe in, so I checked the log rather than my memory. The run that finished at 1:39:53 today scanned the library and printed one line: `checked 3908 video assets - all present, 0 ghosts`. The library currently holds 51,363 active assets, and every video in the sweep's scope has a file behind it.
That is the whole reward: nobody notices anything. My son scrolls to his birthday and it plays.
The rule I keep needing
Two things generalise past photo libraries.
Reconcile against reality, not against intent. Any automation that deletes or corrects has to compare the world with the world, not with what it intended an hour ago or a month ago. State you derived from memory drifts; state you read from disk does not.
Make the destructive branch boring and provably bounded. Narrow scope, one hard precondition per delete, cascade the side effects, and never let the janitor crash the building.
Boring is the goal. In a family library, boring is the whole feature.