From 97edf908891b5149375ffd5d1f77286e9421a799 Mon Sep 17 00:00:00 2001
From: Daniel Dietzler <36593685+danieldietzler@users.noreply.github.com>
Date: Thu, 10 Oct 2024 17:51:54 +0200
Subject: [PATCH] fix: library deletion (#13350)

---
 server/src/services/library.service.ts | 51 ++++++++++++++------------
 1 file changed, 27 insertions(+), 24 deletions(-)

diff --git a/server/src/services/library.service.ts b/server/src/services/library.service.ts
index 84a8a277f5..6544ee3780 100644
--- a/server/src/services/library.service.ts
+++ b/server/src/services/library.service.ts
@@ -341,7 +341,10 @@ export class LibraryService extends BaseService {
 
     this.logger.debug(`Will delete all assets in library ${libraryId}`);
     for await (const assets of assetPagination) {
-      assetsFound = true;
+      if (assets.length > 0) {
+        assetsFound = true;
+      }
+
       this.logger.debug(`Queueing deletion of ${assets.length} asset(s) in library ${libraryId}`);
       await this.jobRepository.queueAll(
         assets.map((asset) => ({
@@ -545,32 +548,32 @@ export class LibraryService extends BaseService {
       }
     }
 
-    if (validImportPaths) {
-      const assetsOnDisk = this.storageRepository.walk({
-        pathsToCrawl: validImportPaths,
-        includeHidden: false,
-        exclusionPatterns: library.exclusionPatterns,
-        take: JOBS_LIBRARY_PAGINATION_SIZE,
-      });
-
-      let count = 0;
-
-      for await (const assetBatch of assetsOnDisk) {
-        count += assetBatch.length;
-        this.logger.debug(`Discovered ${count} asset(s) on disk for library ${library.id}...`);
-        await this.syncFiles(library, assetBatch);
-        this.logger.verbose(`Queued scan of ${assetBatch.length} crawled asset(s) in library ${library.id}...`);
-      }
-
-      if (count > 0) {
-        this.logger.debug(`Finished queueing scan of ${count} assets on disk for library ${library.id}`);
-      } else {
-        this.logger.debug(`No non-excluded assets found in any import path for library ${library.id}`);
-      }
-    } else {
+    if (validImportPaths.length === 0) {
       this.logger.warn(`No valid import paths found for library ${library.id}`);
     }
 
+    const assetsOnDisk = this.storageRepository.walk({
+      pathsToCrawl: validImportPaths,
+      includeHidden: false,
+      exclusionPatterns: library.exclusionPatterns,
+      take: JOBS_LIBRARY_PAGINATION_SIZE,
+    });
+
+    let count = 0;
+
+    for await (const assetBatch of assetsOnDisk) {
+      count += assetBatch.length;
+      this.logger.debug(`Discovered ${count} asset(s) on disk for library ${library.id}...`);
+      await this.syncFiles(library, assetBatch);
+      this.logger.verbose(`Queued scan of ${assetBatch.length} crawled asset(s) in library ${library.id}...`);
+    }
+
+    if (count > 0) {
+      this.logger.debug(`Finished queueing scan of ${count} assets on disk for library ${library.id}`);
+    } else if (validImportPaths.length > 0) {
+      this.logger.debug(`No non-excluded assets found in any import path for library ${library.id}`);
+    }
+
     await this.libraryRepository.update({ id: job.id, refreshedAt: new Date() });
 
     return JobStatus.SUCCESS;