Kareadita/Kavita · warning · KavitaException

delete-library-while-scan

delete-library-while-scan

Error message

delete-library-while-scan

What it means

Thrown by LibraryController.DeleteLibrary when TaskScheduler.HasScanTaskRunningForLibrary reports an active scan task for that library. Message is a localization key 'delete-library-while-scan'; being a KavitaException it surfaces as HTTP 500 unless caught earlier. Logged at Information level as an intentional guard.

Source

Thrown at Kavita.Server/Controllers/LibraryController.cs:564

            }
        }

        return Ok();
    }

    private async Task<bool> DeleteLibrary(int libraryId, int userId, CancellationToken ct = default)
    {
        var series = await unitOfWork.SeriesRepository.GetSeriesForLibraryIdAsync(libraryId, ct: ct);
        var seriesIds = series.Select(x => x.Id).ToArray();
        var chapterIds =
            await unitOfWork.SeriesRepository.GetChapterIdsForSeriesAsync(seriesIds, ct);

        try
        {
            if (TaskScheduler.HasScanTaskRunningForLibrary(libraryId))
            {
                logger.LogInformation("User is attempting to delete a library while a scan is in progress");
                throw new KavitaException(await localizationService.TranslateAsync(userId, "delete-library-while-scan"));
            }

            var library = await unitOfWork.LibraryRepository.GetLibraryForIdAsync(libraryId, ct: ct);
            if (library == null)
            {
                throw new KavitaException(await localizationService.TranslateAsync(userId, "library-doesnt-exist"));
            }


            // 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
            // Aka SeriesRelation has an invalid foreign key
            foreach (var s in await unitOfWork.SeriesRepository.GetSeriesForLibraryIdAsync(library.Id, SeriesIncludes.Related, ct))
            {
                s.Relations = new List<SeriesRelation>();
                unitOfWork.SeriesRepository.Update(s);
            }
            await unitOfWork.CommitAsync(ct);

View on GitHub (pinned to 9c3e540000)

Solutions

  1. Cancel or wait for the in-progress scan task to finish, then retry the delete.
  2. From the UI, stop the scan job for that library before issuing delete.
  3. If a scan task is stuck, investigate/cancel it via the task scheduler rather than forcing the delete.
Defensive patterns

Strategy: validation

Validate before calling

if (TaskScheduler.HasScanTaskRunningForLibrary(libraryId))
    return BadRequest("Library scan in progress; retry after it completes");

Try / catch

try { await DeleteLibrary(id, userId, ct); }
catch (KavitaException ex) { return BadRequest(ex.Message); }

Prevention

When it happens

Trigger: DELETE /api/library on a library that currently has a library scan/refresh task running or queued in the TaskScheduler.

Common situations: User clicks delete while a background scan is mid-flight; a scheduled scan overlaps an admin delete action; a stuck/long-running scan task blocks deletion indefinitely.

Related errors


AI-assisted analysis of Kareadita/Kavita@9c3e540000 (2026-08-13). Data as JSON: /api/errors/1b961b9e2adbc2d0. Report an issue: GitHub.