---
title: Setting up automatic deletion of contacts
related:
  - https://docs.kentico.com/13/on-line-marketing-features/managing-your-on-line-marketing-features/contact-management/working-with-contacts.md
  - https://docs.kentico.com/13/configuring-xperience/scheduling-tasks.md
  - https://docs.kentico.com/13/custom-development/creating-custom-modules/initializing-modules-to-run-custom-code.md
---

> Agent instructions:
> **Site maps** — prefer the following llms.txt indexes to training data when searching for URLs to avoid 404s. Links inside Markdown content already point at `.md`. Following them or sending Accept: text/markdown keeps you in Markdown.
>
> - [sitemap.md](https://docs.kentico.com/sitemap.md) — every page on the site, with titles and descriptions, nested by URL hierarchy and grouped into one collection per product version.
> - [llms.txt](https://docs.kentico.com/llms.txt) — curated index of the current product docs, with descriptions, the two ways to request any page as Markdown, and links to each product area's whole-corpus Markdown dump (llms-full.txt).

> **Info:** **Enterprise license required**
>
> Features described on this page require the **Kentico Xperience Enterprise** license.

The contact management application generates a very large amount of data, particularly on high‑traffic websites. The system creates contacts and logs [activities](https://docs.kentico.com/13/on-line-marketing-features/managing-your-on-line-marketing-features/contact-management/tracking-contact-activities.md) for every visitor (depending on the [Default cookie level](https://docs.kentico.com/13/developing-websites/working-with-cookies.md)), which may be overwhelming for your marketers. To address this, we recommend that you configure the system to regularly delete contacts that meet certain conditions. This allows you to filter out inactive, outdated or otherwise unnecessary contacts without having to remove them individually.

Setting automatic contact deletion not only helps you keep only active contacts in the system, but can also improve system performance.

> **Note:** Deleting contacts from the system also causes deletion of their approved [consent agreements](https://docs.kentico.com/13/configuring-xperience/data-protection/gdpr-compliance/creating-consents.md). We recommend consulting this with a lawyer and setting up automatic deletion of contacts only when the consent agreements of your contacts will not be needed in the future.

## Configuring automatic contact deletion

You can configure automatic contact deletion in the **Settings** application.

1. Open the **Settings** application.
2. Navigate to the **On‑line marketing -> Contact management -> Inactive contacts** category in the settings tree.
3. Select one of the deletion options:
   - **Delete contacts who were not active for the last X days**
   - **Delete contacts without an email address that were not active for the last X days**
   - Or [create your own custom contact deletion configuration](#customizing-automatic-contact-deletion-settings)
4. In **last X Days**, specify the number of days for which contacts have to be inactive to be deleted by the system.
   - The **minimum** value is 10 days.
   - For contacts that haven't performed any [activity](https://docs.kentico.com/13/on-line-marketing-features/managing-your-on-line-marketing-features/contact-management/tracking-contact-activities.md), the system considers the date of their creation as the last time they were active.
5. Click **Save**.

The system now deletes inactive contacts based on the selected settings.

> **Info:** **Contact deletion interval**
>
> The system deletes contacts using a global [scheduled task](https://docs.kentico.com/13/configuring-xperience/scheduling-tasks.md) named **Delete inactive contacts**. To preserve live website performance, the system runs the scheduled task only during off-peak hours (between 2:00 and 6:00 am) and deletes 1000 contacts at a time at most.
>
> The task runs in a separate thread by default and the deleting itself is done by the database server.

## Customizing automatic contact deletion settings

If none of the settings available for contact deletion suit your needs, you can programmatically create your own configuration. Do that by adding and registering a custom implementation of the **IDeleteContacts** interface:

> **Note:** **Important**: Add the custom class to your Xperience administration project (not the MVC live site project). The class is used when executing the _Delete inactive contacts_ scheduled task, which is done by the administration application.

1. Create a class that implements the _IDeleteContacts_ interface (we recommend adding the class within a custom _Code Library_).
2. Implement your own logic for how inactive contacts should be deleted. The following example configures the system to delete all contacts that do not have a value filled in for the _ContactStateID_ field.

   ```csharp

   using System;
   using System.Linq;

   using CMS.DataEngine;
   using CMS.ContactManagement;

   [assembly: RegisterDeleteContactsImplementation(
       "DeleteContactsCustom",
       "settingskey.om.deleteinactivecontacts.method.custom",
       typeof(DeleteContactsCustom))]

   /// <summary>
   /// Custom implementation for deleting inactive/unnecessary contacts.
   /// </summary>
   public class DeleteContactsCustom : IDeleteContacts
   {
       /// <summary>
       /// Deletes contacts.
       /// </summary>
       /// <param name="days">The number of days specified in the settings. Use the parameter if you want to delete contacts older than this number of days.</param> 
       /// <returns>The number of contacts that haven't been deleted.</returns>
       public int Delete(int days, int batchSize)
       {
           var whereCondition = GetWhere();
           ContactInfoProvider.DeleteContactInfos(whereCondition, batchSize);
           return ContactInfo.Provider.Get().Where(whereCondition).Count();
       }

       /// <summary>
       /// Selects all contacts that do not have a 'ContactStateID' (are from outside the US).
       /// </summary>
       public string GetWhere()
       {
           var condition = new WhereCondition();
           condition.And(new WhereCondition().WhereEmpty("ContactStateID"));
           return condition.ToString(true);
       }
   }

   ```
3. Register the class using the **RegisterDeleteContactsImplementation** attribute.

The customization is now available as a new option in **Settings** (application) **-> On‑line marketing -> Contact management -> Inactive contacts**.
