---
title: Setting up automatic deletion of contacts
related:
  - https://docs.kentico.com/k9/on-line-marketing-features/contact-management/working-with-contacts.md
  - https://docs.kentico.com/k9/configuring-kentico/scheduling-tasks.md
  - https://docs.kentico.com/k9/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:** **Kentico EMS required**
>
> Features described on this page require the **Kentico EMS** 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/k9/on-line-marketing-features/contact-management/tracking-contact-activities.md) for every visitor, 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.

## Configuring automatic contact deletion

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

1. Open **Settings** (application) **-> On‑line marketing -> Contact management -> Inactive contacts**.
2. 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)
3. 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/k9/on-line-marketing-features/contact-management/tracking-contact-activities.md) on the site, the system considers the date of their creation as the last time they were active.
4. **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/k9/configuring-kentico/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.
>
> Note that task does _not delete global contacts_.

## 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 implementing the Kentico's _IDeleteContacts_ interface:

1. Create a class that implements the _IDeleteContacts_ interface.
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 'ContactStateID' filled in.

   ```csharp

   using System;
   using System.Linq;

   using CMS.DataEngine;
   using CMS.OnlineMarketing;
   using CMS.SiteProvider;

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

   /// <summary>
   /// Custom implementation for deleting inactive/unnecessary contacts on a site.
   /// </summary>
   class DeleteContactsCustom : IDeleteContacts
   {
       /// <summary>
       /// Deletes contacts on a specific site.
       /// </summary>
       /// <param name="site">The site on which the contacts are deleted.</param>
       /// <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(SiteInfo site, int days, int batchSize)
       {
           if (site == null)
           {
               throw new ArgumentNullException("site");
           }
           var whereCondition = GetWhere(site);
           ContactInfoProvider.DeleteContactInfos(whereCondition, batchSize);
           return ContactInfoProvider.GetContacts().Where(whereCondition).Count();
       }

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

   ```
3. Save the file.

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