Does anyone know how delete/remove a xDB Contact programmatically in Sitecore (8.1+), which also ensures a correct (re)-indexing?
Or, is there another strategy? Such as scrambling the identifier? - or - merging it into void?
Does anyone know how delete/remove a xDB Contact programmatically in Sitecore (8.1+), which also ensures a correct (re)-indexing?
Or, is there another strategy? Such as scrambling the identifier? - or - merging it into void?
Sitecore.Analytics.Data.DataAccess.DataAdapterManager.Provider.DeleteContact(new ID(contactId));
After that you might run the following to update the index:
var index = Sitecore.ContentSearch.ContentSearchManager.GetAnalyticsIndex();
index.Refresh();
But note that it is time and resource consuming.
index.Refresh()
will do anything useful, at all. It will NOT reindex all contacts. The only way to rebuild the Analytics index is to rebuild the Reporting database.
Commented
Nov 17, 2016 at 15:24
index.Refresh()
will not make deleted contacts disappear from the index. You're free to test it yourself if you don't believe me :)
Commented
Nov 18, 2016 at 7:14
Simply deleting a contact is a bad idea. It doesn't take into account so many entities that are related to the contact! Here's a very incomplete list of things to consider:
index.Refresh();
will NOT remove the contact from the index.All in all, it's virtually impossible to clean up every single link to the contact. So sooner or later there will be errors and you won't have any idea where they are coming from.
Do not delete contacts.
Do not delete contacts. Really, just don't. Consider why you want to do it and you'll most likely find there's no reason to.
If you want to "unregister" a contact, you can always remove its identifier and make it anonymous:
ContactRepositoryBase contactRepository = Factory.CreateObject("contactRepository", true) as ContactRepositoryBase;
LeaseOwner leaseOwner = new LeaseOwner("YOUR_WORKER_NAME", LeaseOwnerType.OutOfRequestWorker);
LockAttemptResult<Contact> lockResult = contactRepository.TryLoadContact(email, leaseOwner, TimeSpan.FromMinutes(1));
Contact contact = lockResult.Object;
if (lockResult.Status == LockAttemptStatus.Success)
{
// Make the contact anonymous.
contact.Identifiers.Identifier = null;
contact.Identifiers.IdentificationLevel = ContactIdentificationLevel.Anonymous;
// Save and release the contact.
var options = new ContactSaveOptions(release: true, owner: leaseOwner);
contactRepository.SaveContact(contact, options);
}
If your Analytics index grows too large, consider upgrading to Sitecore 8.1 Update 3 or Sitecore 8.2. There's a setting named ContentSearch.Analytics.IndexAnonymousContacts
that will greatly reduce the amount of indexed contacts.
If you really want to make it seem like the contact doesn't exist anymore, here's what you can do:
using Sitecore.Analytics.Model;
using Sitecore.Analytics.Data.DataAccess;
// ...
ContactRepositoryBase contactRepository = Factory.CreateObject("contactRepository", true) as ContactRepositoryBase;
LeaseOwner leaseOwner = new LeaseOwner("YOUR_WORKER_NAME", LeaseOwnerType.OutOfRequestWorker);
contactRepository.ObsoleteContact(obsoleteContactId, leaseOwner, successorContactId);
The successorContactId
should contain an ID of an existing contact. The contact with obsoleteContactId
will be "merged" into the the successor contact. The benefit of this approach is that the obsolete contact will be scheduled for processing as obsoleted which should remove it from the Analytics index and (hopefully) properly update the RDB statistics.
Do not delete contacts.
contactRepository.ObsoleteContact
method. Can you please clarify what is "YOUR_WORKER_NAME" ?