diff --git a/CRM/Dedupe/Merger.php b/CRM/Dedupe/Merger.php
index fcfa8ed5c580207e76cb1011b2b8137542d7ada3..39f649765f55564baac92d19df3c41ae712f77f3 100644
--- a/CRM/Dedupe/Merger.php
+++ b/CRM/Dedupe/Merger.php
@@ -532,6 +532,12 @@ INNER JOIN  civicrm_membership membership2 ON membership1.membership_type_id = m
         continue;
       }
 
+      if ($table === 'civicrm_activity_contact') {
+        $sqls[] = "UPDATE IGNORE civicrm_activity_contact SET contact_id = $mainId WHERE contact_id = $otherId";
+        $sqls[] = "DELETE FROM civicrm_activity_contact WHERE contact_id = $otherId";
+        continue;
+      }
+
       // use UPDATE IGNORE + DELETE query pair to skip on situations when
       // there's a UNIQUE restriction on ($field, some_other_field) pair
       if (isset($cidRefs[$table])) {
diff --git a/tests/phpunit/api/v3/JobTest.php b/tests/phpunit/api/v3/JobTest.php
index f6a9bfb27c2b705da9cbbf04397303568babbab7..9eb857ff85d9706c41e547577c39ed00b288d110 100644
--- a/tests/phpunit/api/v3/JobTest.php
+++ b/tests/phpunit/api/v3/JobTest.php
@@ -594,6 +594,16 @@ class api_v3_JobTest extends CiviUnitTestCase {
     $this->callAPISuccess('Job', 'process_batch_merge', ['mode' => 'safe']);
   }
 
+  /**
+   * Test that we handle cache entries without clashes.
+   */
+  public function testMergeSharedActivity() {
+    $contactID = $this->individualCreate();
+    $contact2ID = $this->individualCreate();
+    $activityID = $this->activityCreate(['target_contact_id' => [$contactID, $contact2ID]]);
+    $this->callAPISuccess('Job', 'process_batch_merge', ['mode' => 'safe']);
+  }
+
   /**
    * Test the decisions made for addresses when merging.
    *