{"record":{"id":"8b5dd717bb3bda46","repo":"Kareadita/Kavita","slug":"library-doesnt-exist","errorCode":"library-doesnt-exist","errorMessage":"library-doesnt-exist","messagePattern":"library-doesnt-exist","errorType":"exception","errorClass":"KavitaException","httpStatus":400,"severity":"warning","filePath":"Kavita.Server/Controllers/LibraryController.cs","lineNumber":570,"sourceCode":"    private async Task<bool> DeleteLibrary(int libraryId, int userId, CancellationToken ct = default)\n    {\n        var series = await unitOfWork.SeriesRepository.GetSeriesForLibraryIdAsync(libraryId, ct: ct);\n        var seriesIds = series.Select(x => x.Id).ToArray();\n        var chapterIds =\n            await unitOfWork.SeriesRepository.GetChapterIdsForSeriesAsync(seriesIds, ct);\n\n        try\n        {\n            if (TaskScheduler.HasScanTaskRunningForLibrary(libraryId))\n            {\n                logger.LogInformation(\"User is attempting to delete a library while a scan is in progress\");\n                throw new KavitaException(await localizationService.TranslateAsync(userId, \"delete-library-while-scan\"));\n            }\n\n            var library = await unitOfWork.LibraryRepository.GetLibraryForIdAsync(libraryId, ct: ct);\n            if (library == null)\n            {\n                throw new KavitaException(await localizationService.TranslateAsync(userId, \"library-doesnt-exist\"));\n            }\n\n\n            // Due to a bad schema that I can't figure out how to fix, we need to erase all RelatedSeries before we delete the library\n            // Aka SeriesRelation has an invalid foreign key\n            foreach (var s in await unitOfWork.SeriesRepository.GetSeriesForLibraryIdAsync(library.Id, SeriesIncludes.Related, ct))\n            {\n                s.Relations = new List<SeriesRelation>();\n                unitOfWork.SeriesRepository.Update(s);\n            }\n            await unitOfWork.CommitAsync(ct);\n\n            unitOfWork.LibraryRepository.Delete(library);\n\n            var streams = await unitOfWork.UserRepository.GetSideNavStreamsByLibraryId(library.Id, ct);\n            unitOfWork.UserRepository.Delete(streams);\n\n","sourceCodeStart":552,"sourceCodeEnd":588,"githubUrl":"https://github.com/Kareadita/Kavita/blob/9c3e5400007f8a0282f7d883f2ad5e71716e514d/Kavita.Server/Controllers/LibraryController.cs#L552-L588","documentation":"Thrown by LibraryController.DeleteLibrary when GetLibraryForIdAsync returns null for the given libraryId (after the scan-running check). Message is localization key 'library-doesnt-exist'; a KavitaException surfacing as HTTP 500 unless handled earlier.","triggerScenarios":"DELETE /api/library with a libraryId that does not exist in the database — already deleted, wrong id, or stale client state.","commonSituations":"Library was already removed in another session; client holds a stale id; race between two admins deleting the same library.","solutions":["Refresh the library list on the client and confirm the id still exists before deleting.","Treat the error as success if the goal (library gone) is already met.","Pass the correct, current libraryId from the UI."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"var lib = await unitOfWork.LibraryRepository.GetLibraryForIdAsync(id, ct);\nif (lib is null) return NotFound();","typeGuard":null,"tryCatchPattern":"try { await DeleteLibrary(id, userId, ct); }\ncatch (KavitaException) when (/* library-doesnt-exist */) { return NoContent(); }","preventionTips":["Refresh the library list before deleting.","Treat 'already gone' as success.","Pass the current libraryId from fresh UI state."],"tags":["library","validation","not-found","state"],"backgroundTag":null,"analyzedSha":"9c3e5400007f8a0282f7d883f2ad5e71716e514d","analyzedAt":"2026-08-13T19:06:05.897Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}