Skip to content

fix: an audiobook request delete removes only the files that request placed - #551

Merged
kikootwo merged 12 commits into
mainfrom
fix/526-audiobook-delete-scope
Nov 2, 2026
Merged

kikootwo merged 12 commits into
mainfrom
fix/526-audiobook-delete-scope

Conversation

@kikootwo

@kikootwo kikootwo commented Nov 2, 2026 •

Copy link
Copy Markdown
Owner

Summary

Deleting an audiobook request with its files removed the whole book folder. The check before it only proves that the folder's audio is this import, so everything else in the folder went with it: the ebook the same book's ebook request placed there, the user's own files, NAS and Audiobookshelf leftovers.

Now the delete removes the audio of that import, and the folder only once nothing else is in it. Whatever stays is listed in the result and in the admin table's warning toast.

Changes

  • src/lib/utils/book-folder-delete.ts (new): the audiobook delete inside a folder the guard allows.
    • Removes the audio directly in the folder, and only when that listing still hashes to the import's filesHash (the same set the guard hashed; any change keeps everything).
    • Removes RMAB's partial copies older than 24 h.
    • Removes cover.jpg (a regular file, never a link) and the junk list only when nothing else is left but those and ebooks recorded by active ebook requests.
    • Never enters a subfolder. The folder goes only when empty (non-recursive rmdir).
    • Audio unlink failure: stops, nothing else changes, as before (<name> could not be removed (...), or "partly removed" after the first file). ENOENT counts as already gone. Every rmdir error is ignored; a non-ENOTEMPTY one of the emptied folder is logged and returned as folderKept.error (message Request deleted; the emptied book folder could not be removed (EBUSY)), never listed as something the folder holds.
    • cover.jpg and junk kept only because other files stay beside them are labelled kept with the other files.
  • src/lib/utils/book-folder-junk.ts (new): the junk list (.DS_Store, Thumbs.db, desktop.ini, ._*, metadata.json, metadata.abs; folders @eaDir, .@__thumb) and a limited walk for the folders: lstat only, links never followed, at most 3 levels and 500 entries, and any link, special file, audio or ebook file keeps the whole folder. Each walked folder is re-checked before it is emptied and stays open until its own removal, so on a local filesystem a folder made in its place can't pass that check with a reused inode number (Linux reuses a freed one at once). A swap after the check, or a filesystem that keeps a folder's number by path (mergerfs, NFS), is not covered; only junk-list names are removed either way.
  • src/lib/services/request-delete-files.ts: D1 calls the above (a symlinked title folder still loses only the link). D2 (ebook delete): when the folder it unlinked a recorded file from holds nothing but the junk list, it also unlinks those junk files, empties the junk folders (the same limited walk), removes the folder and sweeps empty parents. A failed rmdir of that folder is only logged: the recorded file is gone, so it is not "files kept" (filesDeleted: true, success toast).
  • src/lib/services/request-delete-children.ts (new), request-delete.service.ts:
    • A delivered ebook request of the same book is no longer soft-deleted with the audiobook request. It stays with parentRequestId cleared, so select-ebook, the download and downloadAvailable keep working.
    • The book row keeps that ebook's folder and format (what an ebook-only import leaves); filesHash and the library link are cleared and the status goes back to requested, as before. With several delivered ebook requests, the newest (createdAt desc) whose recorded ebook is on disk.
    • Undelivered ebook requests are soft-deleted as before.
    • An ebook delete that removed the book's stored folder clears the stored path.
    • New folderKept result: { folder, files: [{ name, why }], more } (first 20).
  • src/app/api/admin/requests/[id]/route.ts, RecentRequestsTable.tsx: details.folderKept; the warning toast shows the message.
  • organize-files.processor.ts (createEbookRequestIfEnabled), find-missing-ebooks.processor.ts: "this book already has an ebook request" is checked by book (audiobookId), not by parent. Without it, an ebook request that outlived its audiobook request would get a second one beside it after a re-request.
  • createEbookRequestIfEnabled first calls relinkParentlessEbookRequest (request-delete-children.ts), whatever the ebook settings: the book's newest parentless active ebook request gets the new audiobook request as its parent (compare-and-set on parentRequestId: null, so of two imports only one claims it; skipped when the new request already has an ebook request). Fetch e-book, select-ebook and interactive search look the ebook request up by parent, so after a re-request they find the survivor instead of opening a second one. The survivor can be another user's ebook request on the same book, as main's ebook linking by book already allows.
  • reported-issue.service.ts: a replace keeps the book's filePath/fileFormat (which the delete pointed at the surviving ebook's folder) while a delivered ebook request of the book remains and the old request's delete succeeded; otherwise it clears them as before.
  • library-delete-guard.ts: header wording only; the guard's checks are unchanged.
  • Docs: request-deletion.md (D1 table, junk list, child rule, folderKept, Fixed Issues Deleting an audiobook request also deletes the ebook and other non-audio files in its book folder #526), ebook-sidecar.md, file-organization.md.

What stays and why

In the book folder After the audiobook delete Listed as
The ebook an active ebook request recorded (#497) stays ebook of another request
Older ebooks with no record, user files, subfolders, links stay, never entered RMAB has no record that it placed it
cover.jpg, .DS_Store, @eaDir/, metadata.json... go when nothing but these and recorded ebooks is left; otherwise stay kept with the other files
A partial copy newer than 24 h stays an import is still writing it

Deleting both requests, in either order, leaves no folder. A book whose ebook was imported before #497 (no record) keeps that ebook and its cover, listed, as #497 already keeps it.

Tests

  • New tests/services/request-delete.book-folder.test.ts (12, real temp folders). Covers: audio + cover + recorded epub (the ebook request stays, unlinked; the book keeps the ebook's folder); a user file keeps the cover; junk only; @eaDir holding a link; legacy ebook; a subfolder; stale vs fresh partial; a symlinked title folder; an undelivered child; both delete orders; a pre-Deleting an ebook request removes ebook files RMAB didn't download #497 book.
  • New tests/utils/book-folder-junk.test.ts (31): the junk names, the walk's limits, a folder deleted and made again or moved aside before removal, the D1 plan (unreadable folder, unhashed audio, recorded ebook through a symlinked path), the D2 empty-folder step.
  • Added: request-delete.library-guard.test.ts (later-file failure, ENOENT, rmdir EBUSY); organize-ebook-on-disk.test.ts (no second ebook request beside a parentless one); find-missing-ebooks.processor.test.ts (the lookup is by book); admin-requests.routes.test.ts (folderKept passed through).
  • New tests/processors/organize-ebook-relink.test.ts (6): the real organize processor and the real fetch-ebook route over one in-memory requests table. A re-request's import links the survivor and creates none (also with auto-grab off); Fetch e-book on the new request then answers "E-book request already exists (status: downloaded)" with the survivor's id and creates nothing; a survivor another import claims between lookup and write (compare-and-set), or one already linked elsewhere, is left alone; nothing is linked when the new request already has its own ebook request.
  • Added: request-delete.book-folder.test.ts (two delivered ebook requests in different folders: the book keeps the newest one's); reported-issue.replace-guard.test.ts (the replace keeps the stored path while a delivered ebook request remains, with or without a record; clears it for an undelivered one, or when the delete itself failed); request-delete.ebook-placement-guard.test.ts (a failed rmdir of the emptied ebook folder: success, no filesKeptReason).
  • On main (src at 1af0e12, the new modules absent), the new and changed tests give 14 failed / 83 passed of 97 across 5 files, and book-folder-junk.test.ts (29) does not load. Per file: book-folder 8 of 12, library-guard 3 of 25, admin routes 1 of 29, find-missing-ebooks 1 of 22, organize-ebook-on-disk 1 of 9.
  • The files changed in the second round, with src at 432e7da (main): 19 failed / 55 passed of 74 across 5 files (relink 5 of 6, replace 2 of 8, book-folder 9 of 13, library-guard 3 of 25; the ebook-placement file passes, since main never removes an ebook folder). With src at e353e42 (the first round): 14 failed, every new or changed test of the second round except the four that pin unchanged behavior (a survivor linked elsewhere, a request with its own ebook request, an undelivered ebook request, a failed delete).

Changed existing assertions:

  • request-delete.book-folder.test.ts (5 cases): a cover.jpg kept beside user files is listed as kept with the other files (was RMAB has no record that it placed it).
  • request-delete.library-guard.test.ts rmdir EBUSY: folderKept has no files and error: 'could not be removed (EBUSY)', message Request deleted; the emptied book folder could not be removed (EBUSY) (was the folder Book/ listed as its own content).
  • e2e 526-audiobook-delete.spec.ts 526a: select-ebook must answer 400 with "already exists" (was: anything but 404); 526b: the toast text has the cover's new label.
  • request-delete.library-guard.test.ts "removes a default-template folder": the folder no longer holds an unrecorded Book.epub (that file now stays; covered by the new legacy-ebook case).
  • request-delete.library-guard.test.ts EACCES case: the failure is injected into unlink (was rm). The reason is now Book.m4b could not be removed (permission denied) (was "the folder was partly removed..."), and the audio and cover are checked to still be there. The "partly removed" reason is now asserted for a failure on the second file.
  • e2e library-delete-guard.spec.ts E-a and E-b: the folder stays holding the ebook request's epub (was removed); E-a's book row keeps the ebook's folder and fileFormat epub (was cleared).
  • e2e library-delete-guard.spec.ts R6: the root-owned subfolder is kept and listed while the audio goes, the ABS item is deleted and the row cleared (was: delete refused as partly removed, ABS item and row kept).

Checks

  • npm run test (head 42fadc3, Linux, Node 20 container): 9306 passed, 72 skipped, 0 failed.
  • The last round's changed tests and every test file that imports a changed file: 16 files, 241 passed on Linux and on Windows.
  • New and touched test files, plus every test file that imports a file changed in the second round: 26 files, 486 passed.
  • npx tsc --noEmit and the e2e tsconfig: clean.
  • eslint on the changed files: no new findings (the remaining ones are on main already).
  • Image build (dockerfile.unified, at 42fadc3): passes.
  • e2e on that image: smoke 1, 526-audiobook-delete 2; all pass. 526a: select-ebook on the surviving ebook request answers 400 "E-book request already exists (status: downloaded)".
  • First round, image at 565a2ed: library-delete-guard 15, 497-ebook-delete 3, 242-delete-guard 12 (media bind), library-delete-plex 4 (Plex backend); all pass. The audiobook delete leaves the ebook request's epub byte for byte (sha256 a9a51fd9... before and after), and in the user-file case the epub, cover and notes.txt are unchanged and Book.m4b is the only path removed under /media.

Risk and rollback

  • Deletes less than before, by design: folders holding anything besides the import's audio, its cover and junk now stay (listed in the toast). A delivered ebook request outlives its audiobook request.
  • The audiobook delete (D1) never removes more than main: every removal is a direct entry of a folder that passed the same guard main's whole-folder delete needed, by name, with no recursion outside the junk walk.
  • The ebook delete (D2) removes more than main: main removed only the recorded ebook files. Now, when nothing else is left in that ebook folder, it also removes the junk files (.DS_Store, Thumbs.db, desktop.ini, ._*, metadata.json, metadata.abs) and junk folders (@eaDir, .@__thumb), then the emptied folder and empty parents. A user file named exactly like a junk entry, alone in a separate ebook folder, goes with it.
  • "This book already has an ebook request" is now checked by book, so it also sees other users' ebook requests on the same book. One user's cancelled or denied ebook request means no automatic ebook request for another user's audiobook request of that book, and a parentless failed one is reset and retried by the scheduled check. One ebook per book folder; on main each user's audiobook request got its own.
  • Other users' audiobook requests for the same book still lose the shared audio (the duplicate check is per user, request-creator.service.ts). Same as main; left for a follow-up.
  • A cover.jpg the user customized, in a folder with nothing else of theirs, is removed with the audio.
  • No schema change. Rollback: revert the commit.

Limits

  • A delivered ebook request imported before placement records existed survives the audiobook delete with the book's filePath null: its download answers 404 until the book is imported again.
  • Another user's completed audiobook request on the same book shows a Download button that answers 404 after the delete (the stored path now points at the ebook folder; the download serves audio only). Nothing leaks.
  • A request that a library match marks available without an import never runs the relink, so a manual Fetch e-book on it can still open a second ebook request beside the survivor.
  • The relink takes the newest parentless ebook request whatever its status. If that is a cancelled or denied one, Fetch e-book on the new request answers that it already exists instead of opening a new one.
  • An AppleDouble file with an audio extension (._Book.m4b) counts as audio, so a folder holding one fails the hash check and is kept whole (as on main).

Screenshots

Closes #526

🤖 Generated with Claude Code

The admin toast after deleting an audiobook request whose folder also holds another request's ebook, the cover and a user file (1280 px, light):

Evidence: https://gist.github.com/kikootwo/5eafe6cc6276611667ed2b992bddd8b9

Closes #526

🤖 Generated with Claude Code

kikootwo and others added 12 commits November 2, 2026 13:16
…placed

The audiobook delete no longer removes the whole book folder. It removes
the audio the import hashed, RMAB's stale partial copies, and cover.jpg
plus OS and Audiobookshelf leftovers only when nothing else is left but
ebooks recorded by active ebook requests. The folder goes only when empty;
the result lists what stayed (folderKept) and the admin table shows it.

A delivered ebook request of the same book stays, unlinked, and the book
keeps its ebook's folder and format. An ebook request delete removes a
folder it leaves holding only junk and clears the stored path if it was
that folder. The automatic and scheduled ebook request checks look by book.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Request deletion, ebook sidecar and file organization docs describe the
new delete. The e2e library delete guard cases E-a, E-b and R6 now expect
the folder to stay with what it still holds; a new #526 spec covers the
ebook request that stays, the delete in both orders and the warning toast.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ebook sibling

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ete-scope

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…lete

A delivered ebook request stays, unlinked, after its audiobook request is
deleted. The next audiobook import of the book now sets its parent to the
new request (compare-and-set on a null parent, skipped when the new
request already has an ebook request), so Fetch e-book, select-ebook and
interactive search find it by parent instead of opening a second one.

Also:
- the stored ebook folder kept for the book is the newest delivered
  ebook request's (ordered by createdAt)
- a reported-issue replace keeps the stored path and format while a
  delivered ebook request of the book remains
- a failed rmdir of the folder an ebook delete emptied is logged, not
  reported as files kept
- a failed rmdir of the emptied book folder is reported as the folder's
  error (folderKept.error), not listed as something the folder holds
- cover.jpg and junk kept only because other files stay beside them are
  labelled "kept with the other files"

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- file-organization.md, request-deletion.md: the ebook request delete
  also removes the junk files and junk folders of a folder nothing else
  is left in, then the emptied folder and empty parents (before, only
  the recorded files). The audiobook delete never removes more than the
  former whole-folder delete.
- The by-book ebook request check also sees other users' ebook requests
  on the book; a re-request's import links the surviving ebook request.
- Limits: a delivered legacy ebook child keeps no stored path; another
  user's Download button 404s after the delete; an AppleDouble file with
  an audio extension counts as audio.
- e2e 526a: select-ebook must answer 400 "already exists"; 526b expects
  the cover's new label.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…succeeded

When deleteRequest fails, the book row may still point at the old audio
folder, so the replace clears the stored path as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Linux gives a folder made right after a delete the freed inode number,
so the junk walk's dev/ino re-check could pass on a different folder.
Each walked folder now stays open from the walk until its own rmdir,
which keeps its inode number taken; every plan is released on all paths.

The request delete service tests now mock the book folder step and give
the guard one path on every OS: on Windows they had taken the link-only
branch, on Linux the folder step with an fs mock that lacks readdir.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kikootwo
kikootwo marked this pull request as ready for review November 2, 2026 22:21
@kikootwo
kikootwo merged commit 84ae2ec into main Nov 2, 2026
1 check passed
@kikootwo
kikootwo deleted the fix/526-audiobook-delete-scope branch November 2, 2026 22:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Deleting an audiobook request also deletes the ebook and other non-audio files in its book folder

1 participant