From eb7777639d70a584702b679888411fab07c36678 Mon Sep 17 00:00:00 2001
From: Mert <101130780+mertalev@users.noreply.github.com>
Date: Thu, 5 Sep 2024 19:09:19 -0400
Subject: [PATCH] fix(server): clean face tables after delete (#12375)

clean face tables after delete
---
 server/src/repositories/person.repository.ts | 21 ++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)

diff --git a/server/src/repositories/person.repository.ts b/server/src/repositories/person.repository.ts
index 7459ca3183..1290df740e 100644
--- a/server/src/repositories/person.repository.ts
+++ b/server/src/repositories/person.repository.ts
@@ -6,6 +6,7 @@ import { AssetFaceEntity } from 'src/entities/asset-face.entity';
 import { AssetJobStatusEntity } from 'src/entities/asset-job-status.entity';
 import { AssetEntity } from 'src/entities/asset.entity';
 import { PersonEntity } from 'src/entities/person.entity';
+import { SourceType } from 'src/enum';
 import {
   AssetFaceId,
   DeleteAllFacesOptions,
@@ -53,16 +54,20 @@ export class PersonRepository implements IPersonRepository {
   }
 
   async deleteAllFaces({ sourceType }: DeleteAllFacesOptions): Promise<void> {
-    if (sourceType) {
-      await this.assetFaceRepository
-        .createQueryBuilder('asset_faces')
-        .delete()
-        .andWhere('sourceType = :sourceType', { sourceType })
-        .execute();
-      return;
+    if (!sourceType) {
+      return this.assetFaceRepository.query('TRUNCATE TABLE asset_faces CASCADE');
     }
 
-    await this.assetFaceRepository.query('TRUNCATE TABLE asset_faces CASCADE');
+    await this.assetFaceRepository
+      .createQueryBuilder('asset_faces')
+      .delete()
+      .andWhere('sourceType = :sourceType', { sourceType })
+      .execute();
+
+    await this.assetFaceRepository.query('VACUUM ANALYZE asset_faces, face_search');
+    if (sourceType === SourceType.MACHINE_LEARNING) {
+      await this.assetFaceRepository.query('REINDEX INDEX face_index');
+    }
   }
 
   getAllFaces(