Deduplication & Merging
When leads arrive from web forms, email imports, portal signups, and manual entry, duplicate records are inevitable. Contacts prevents most duplicates at write time through ContactDeduplicator and resolves remaining clusters atomically via ContactMergeService.
Duplicate Detection (ContactDeduplicator)
Section titled “Duplicate Detection (ContactDeduplicator)”Open Setup -> Contacts -> Duplicates (requires contacts-admin) to review candidate clusters grouped by confidence level, choose a primary record, and merge duplicates cleanly.
Matching Strategies
Section titled “Matching Strategies”- Live Upsert Strategy (
dedupe_strategy):email_then_phone(default): Matches normalized lowercaseprimary_emailfirst, then falls back to E.164phone_e164.email_only: Matches strictly onprimary_email.
- Cluster Detection (
findDuplicateGroups()):- Primary Exact: Identical normalized
primary_emailorphone_e164(100% Match). - Secondary Channel (
includeChannels: true): Matches overlapping email or phone entries insidecontacts-channelrows. - Fuzzy Name + Organization/Domain (
includeFuzzy: true): Opt-infuzzy_name_orgclusters (75% Match) shown strictly for manual review (never auto-merged by CLI).
- Primary Exact: Identical normalized
- Organization Matching:
findOrganizationByTaxId(),findOrganizationByDomain()(automatically ignores free consumer email domains viaisFreeEmailDomain()), andfindByExternalRef().
Atomic Merging (ContactMergeService)
Section titled “Atomic Merging (ContactMergeService)”When you merge a duplicate contact into a primary contact (./merge/ or $contacts->mergeContacts()):
- Fires
Contacts::beforeMerge('contact', $primary, $duplicate, $strategy). - Merges missing scalar fields (respecting allowlisted
prefer_duplicate_fieldsoverrides). - Combines and deduplicates
contacts-channel,contacts-address,contacts-membership,contacts-consent,roles,tags,domain_meta, andexternal_refs. - Reassigns all
contacts-notesreferencing$duplicate->idto$primary->id. - Repoints embedded
@fieldset:contacts-profile.linked_contact_id(page_id>0) references on ProcessWire Pages and Users. - Deletes the duplicate contact inside
Contacts::runInTransaction(). - Fires
Contacts::afterMerge('contact', $primary, $duplicateId, $stats)post-commit so all'contactsParticipant' => truemodules repoint their stored foreign UUIDs.
mergeOrganizations() performs the equivalent atomic merge for organizations, reassigning child organizations (reassignedChildren), member contacts (reassignedMembers), notes (reassignedNotes), and embedded @fieldset:contacts-organization / @fieldset:contacts-memberships references.
<?php
declare(strict_types=1);
namespace ProcessWire;
$contacts = Contacts::requireInstance($this);
$stats = $contacts->mergeContacts($primaryContact, $duplicateContact, [ 'prefer_duplicate_fields' => ['phone_e164', 'job_title'],]);CLI Deduplication (contacts:dedupe)
Section titled “CLI Deduplication (contacts:dedupe)”# Preview exact + fuzzy duplicate clusters without mutating recordsphp vendor/bin/wire contacts:dedupe --scope=contacts --fuzzy --dry-run --json
# Automatically merge exact primary clusters into the earliest primary recordphp vendor/bin/wire contacts:dedupe --scope=contacts --auto-merge --force --json