diff --git a/process/DuplicateScanner_lib/process.js b/process/DuplicateScanner_lib/process.js
index 53c5403b600d31c6a172a7278d688c56b5575f9c..f94019bc937c507935f270850d7170706833e9d9 100644
--- a/process/DuplicateScanner_lib/process.js
+++ b/process/DuplicateScanner_lib/process.js
@@ -50,7 +50,16 @@ DuplicateScannerUtils.loadFilters = function(pFilterName, pTargetEntity)
  */
 DuplicateScannerUtils.DeleteCachedDuplicate = function(pDuplicateId)
 {
-    var query = newSelect("count(ID), CLUSTERID from DUPLICATECLUSTERS")
+
+    var query = newSelect("count(ID)")
+                    .from("DUPLICATECLUSTERS")
+                    .where("DUPLICATECLUSTERS.CLUSTERID", newSelect("CLUSTERID")
+                                                            .from("DUPLICATECLUSTERS")
+                                                            .where("DUPLICATECLUSTERS.DUPLICATEID", pDuplicateId).build(),
+                                                        SqlBuilder.IN())
+                    .and("DUPLICATECLUSTERS.DUPLICATEID", pDuplicateId, SqlBuilder.NOT_EQUAL());
+
+var test = newSelect("CLUSTERID")
                     .from("DUPLICATECLUSTERS")
                     .where("DUPLICATECLUSTERS.CLUSTERID", newSelect("CLUSTERID")
                                                             .from("DUPLICATECLUSTERS")
@@ -58,10 +67,20 @@ DuplicateScannerUtils.DeleteCachedDuplicate = function(pDuplicateId)
                                                         SqlBuilder.IN())
                     .and("DUPLICATECLUSTERS.DUPLICATEID", pDuplicateId, SqlBuilder.NOT_EQUAL())
                     .groupBy("CLUSTERID");
+    
+    var so = "select count(ID),( select CLUSTERID from DUPLICATECLUSTERS)"
++ " from DUPLICATECLUSTERS where DUPLICATECLUSTERS.CLUSTERID in"
++ " (select CLUSTERID from DUPLICATECLUSTERS where(DUPLICATECLUSTERS.DUPLICATEID = ('" + pDuplicateId + "')))"
++ " and("
++ " DUPLICATECLUSTERS.DUPLICATEID <> ('" + pDuplicateId + "')"
++ " )"
++ " group by CLUSTERID"
+
 
     var countAndClusterId = query.arrayRow();
+    var testundso = test.arrayRow()
     let countDuplicatesInClusterWithoutParameterId = countAndClusterId[0];
-    let clusterId = countAndClusterId[1];
+    let clusterId = testundso[0];
 
     //If only one duplicate would be remaining,
     //the whole cluster has to be deleted because there are no more duplicates.
@@ -70,9 +89,10 @@ DuplicateScannerUtils.DeleteCachedDuplicate = function(pDuplicateId)
     if(countDuplicatesInClusterWithoutParameterId <= 1)
     {
         let deleteStatements = [];
+        if(clusterId != null && clusterId != undefined){
         deleteStatements.push(["DUPLICATECLUSTERS", newWhere("DUPLICATECLUSTERS.CLUSTERID", clusterId).build()]);
         deleteStatements.push(["UNRELATEDDUPLICATES", newWhere("UNRELATEDDUPLICATES.CLUSTERID", clusterId).build()]);
-
+        }
         db.deletes(deleteStatements);
     }
     else
@@ -635,9 +655,9 @@ DuplicateScannerUtils.TranslateEntityToIndexFields = function(pEntityName, pEnti
  */
 DuplicateScannerUtils.MergePerson = function(pSourceContactId, pTargetContactId)
 {
-    let updateStatementsCurrentAlias = [];
-    let updateStatementsSystemAlias = [];
-    let deleteStatements = [];
+    var updateStatementsCurrentAlias = [];
+    var updateStatementsSystemAlias = [];
+    var deleteStatements = [];
 
     var sourcePersonId = newSelect("PERSON_ID")
                             .from("CONTACT")
@@ -656,9 +676,9 @@ DuplicateScannerUtils.MergePerson = function(pSourceContactId, pTargetContactId)
     deleteStatements = deleteStatements.concat(_DuplicateScannerUtils._buildDeletePersonAndContactQuery(sourcePersonId, pSourceContactId));
     deleteStatements = deleteStatements.concat(_DuplicateScannerUtils._buildDeleteCachedUnrelatedDuplicateQuery(pSourceContactId));
 
-    let affectedRowsCurrentAlias = db.updates(updateStatementsCurrentAlias);
-    let affectedRowsSystemAlias = db.updates(updateStatementsSystemAlias, SqlUtils.getSystemAlias());
-    let deletedRows = db.deletes(deleteStatements)
+    var affectedRowsCurrentAlias = db.updates(updateStatementsCurrentAlias);
+    var affectedRowsSystemAlias = db.updates(updateStatementsSystemAlias, SqlUtils.getSystemAlias());
+    var deletedRows = db.deletes(deleteStatements)
 
     DuplicateScannerUtils.DeleteCachedDuplicate(pSourceContactId);