Skip to content

0013. Deleted videos wait in a trash: a tombstone row and moved files, not a soft-delete flag

Status: Accepted

Context

Deleting a video removed its rows and then its media file for good, and bulk delete made a slip expensive. Users wanted deleted videos to sit somewhere for a week, restorable, with a list, a size and an "Empty trash" button.

The obvious design is a trashed_at column on videos. Every reader of videos would then have to filter it: the library list, FTS5 search, the tag table, the vector index and recommendations, related videos, playlists, history, the channel feed and catalog "in library" flags, stats, metadata sync, the AI and compat workers, sprites and the scanner. A single missed query shows a deleted video, plays it, spends AI tokens on it or counts it twice, and the unique file_path and youtube_id of a trashed row would block a new download of the same video.

Decision

  • A tombstone, not a flag. Trashing a video (services/trash.trash_video) runs the ordinary delete (delete_video_cascade), so every table forgets it exactly as before. Before that, it writes one trash_entries row (migration 0010_trash) holding the original id, title, channel, YouTube id and path, the size, who deleted it and when, a list of moved files (files_json) and a JSON snapshot of what a restore needs: the videos row, video_ai_meta, the embeddings (base64), playlist places (and which playlists used it as cover), every user's user_video_state and the chat messages.
  • Files are moved, not copied. Media, subtitles and the H.264 copy go to DOWNLOADS_DIR/.horde-trash/<folder>/ and thumbnails and sprites to DATA_DIR/trash/<folder>/, each under its path relative to its root. <folder> is a random token stored on the entry (trash_entries.folder), never the row id: an id is reused when an insert is rolled back, and a folder shared by two trashes lets one erase the other's files. The media move is a plain rename; when the trash folder is on another filesystem the delete is refused (409 trash_cross_device) rather than copying gigabytes inside a request. Thumbnails and sprites (small, local) may be copied. The scanner, the temp sweep and orphan cleanup ignore the folder.
  • Order of a trash. (1) Write a manifest (video id, path, file list) into each trash folder; (2) move the files, with no database transaction open, so a slow move never holds SQLite's single write lock; (3) one short transaction inserts the entry, checks the video row is still there, and deletes the video. If a move or the commit fails, the files go back; if putting some back fails too, the folder and its files stay (and are reported) instead of being deleted. Restore runs in the same order: files first, then one transaction that deletes the entry (checking it deleted exactly one row) and inserts the rows.
  • Restore moves the files back and re-inserts the rows with the original video id, skipping playlists and users that no longer exist. A thumbnail, sprite or compat copy regenerated at its old path while the video was away is kept and the trashed copy dropped; only the media path is a real conflict. video_tags and videos_fts are never written directly: they are refilled by the SQLite triggers when the videos and video_ai_meta rows are inserted, and the youtube id by the mapper listener. It refuses with 409 trash_conflict when the old media path is occupied, a library video has the same YouTube id (downloaded again) or a download of that video is queued or running. The scanner is told the media path is held (a counted hold that downloads' own marks never touch) during the move so it does not import the file as a new video.
  • Retention. A trash-purge worker erases entries older than HORDE_TRASH_DAYS (default 7) every hour, by age only: an entry whose files cannot be found is not forgotten early, and an entry whose trash folder cannot be seen at all (an unmounted share) is left alone until the folder is back, so an outage cannot cut the 7 days short. HORDE_TRASH_DAYS=0 turns the trash off for new deletes; entries already in it still expire on the default 7-day schedule.
  • The sweep never deletes media. A trash folder no entry owns (a crash between the move and the commit) is looked at once it is an hour old. If its manifest names a video that is still in the library, the files are moved back (a regenerated thumbnail wins over the trashed copy) and the folder removed; otherwise the folder is left in place and logged once. Only folders of committed entries are ever removed, by the purge, Empty trash and Delete forever.
  • Which deletes. Only the user's deletes of a video together with its file (single and bulk, and one service function for callers such as future retention rules). Keeping the file, the scanner dropping rows of missing files, replacement by a re-download, factory reset and account deletion stay permanent.
  • Disk. Trashed bytes count toward the library size limit and the Storage total; erasing them wakes held downloads.
  • Auto-download. Deleting a video (the trash move, and the permanent delete when the trash is off) records a deleted row in channel_catalog_skips for the catalogs of its channel, through one function (channel_catalog/skips.record_deleted_video) that any future retention rule calls too. Auto-download honours it forever, also after the trash is emptied. Unlike members_only skips it does not hide the video: the feed, related and next-part views keep listing it as not downloaded. A restore, and any manual or playlist download of the video, removes it.
  • Access. Everything needs the delete_videos permission.

Consequences

  • No query in the application changes: a trashed video is simply not a video. The cost is a restore routine that must know every table that hangs off videos (a new dependent table needs adding to the snapshot, or its rows are lost on restore).
  • A restore returns the same id, so links, history and playlist places work again. Rows that are history rather than ownership (download jobs, the OpenRouter cost ledger, pending AI jobs) are not restored.
  • The snapshot of a video with many embedding chunks can reach a megabyte of JSON in the database for up to a week; listings never read it.
  • The trash is a visible folder on the media share (see Storage layout) and its size is not free: the library limit and the disk-space floor see it.
  • A crash between moving the files and the commit leaves a library row whose files sit in a trash folder without an entry; the next purge after an hour moves them back by itself. The price of never erasing an unowned folder is that a folder whose entry was erased while its share was unreachable stays on disk until someone removes it by hand (it is logged once).
  • The trash folder must be on the same filesystem as the media. That holds by construction unless a channel folder under DOWNLOADS_DIR is its own mount.