# Contact lifecycle

Contacts belong to your account, not to a mailing list. Each contact has a state, such as active
or deleted, and a separate membership on each list it joins. A contact that leaves a list is not
deleted, and creating a contact with the address of a deleted one does not give you a new
contact.

## Contact states

<div className="[&_table]:w-full [&_th:first-child]:w-36">

| State         | What it means                                                                                                                             |
| ------------- | ----------------------------------------------------------------------------------------------------------------------------------------- |
| Active        | Gets mail from the lists it is a member of.                                                                                               |
| Unconfirmed   | Waits for double opt-in. Gets no mail from lists, and is not counted as a list member.                                                    |
| Deleted       | Has left all its lists, but keeps its properties and can be restored.                                                                     |
| Pseudonymized | Deleted by the [daily cleanup](#daily-cleanup). Its email address is replaced with a hash, and its SMS number and properties are removed. |
| Forgotten     | Its personal data was erased on request. Cannot be restored.                                                                              |

</div>

[`getRecipient`](/api/contacts#get-contact) and
[`getDeletedRecipient`](/api/contacts#get-deleted-contact) show the state in the `recipient`
fields. `enabled` is `true` only for active and unconfirmed contacts, `confirmed` is `false` for
an unconfirmed contact, and `forgotten` is `true` for a forgotten one. A pseudonymized or
forgotten contact has a hash as its `email`.

A contact becomes confirmed when you call [`confirmRecipient`](/api/confirmation#confirm-contact),
when you add it to a list with [`joinMailingList`](/api/list-members#add-contacts-to-list) without
`noauto`, when you create it with `autoconfirm`, or when a list import adds it.

`membership` in the `getRecipient` response shows the lists the contact is on or has been on, with
the list ID and name, the join time and the unsubscribe time. A contact that has left a list has an
unsubscribe time for it, and stays active on its other lists.

### Daily cleanup

:::warning

Once a day, LianaMailer deletes and pseudonymizes active contacts that have not been on any
mailing list for over a week. Contacts that were in a delivery other than an event or welcome mail
during the past week are kept. To keep the contacts you create, add them to a list, for example
with [`createAndJoinRecipient`](/api/contacts#create-or-update-contact).

:::

## Leaving a list or deleting

<div className="[&_table]:w-full [&_table]:table-fixed [&_th:first-child]:w-28 [&_th_code]:break-all [&_th_code]:whitespace-normal">

|            | [`removeRecipientFromMailingList`](/api/list-members#remove-contact-from-list) | [`deleteRecipient`](/api/contacts#delete-contact) | [`forgetRecipient`](/api/contacts#forget-contact) |
| ---------- | ------------------------------------------------------------------------------ | ------------------------------------------------- | ------------------------------------------------- |
| Contact    | Stays active                                                                   | Deleted and unconfirmed                           | Forgotten                                         |
| Lists      | Leaves one list, or all with `list_id` `0`                                     | Leaves all lists                                  | Leaves all lists                                  |
| Properties | Kept                                                                           | Kept                                              | Erased                                            |
| Consents   | Kept                                                                           | Revoked                                           | Erased                                            |
| Undo       | `joinMailingList`                                                              | Restore it, then rejoin lists                     | Not possible                                      |

</div>

[`quitMailingList`](/api/list-members#leave-mailing-list) is deprecated. It removes a contact only
from a list it has a membership on. Use `removeRecipientFromMailingList` instead.

When a contact leaves a list, including when it is deleted, LianaMailer saves the time and the
reason. [`getUnsubscriptions`](/api/list-members#get-unsubscriptions) returns them, even for
contacts that have since rejoined the list.

## Restoring a deleted contact

[`enableRecipient`](/api/contacts#restore-contact-by-id) and
[`reactivateRecipient`](/api/contacts#restore-contact-by-email-sms) both set `enabled` back to
`true` and keep the contact's ID and properties. Neither restores its list memberships, or the
consents that were revoked when it was deleted. `getDeletedRecipient` shows the lists the contact
left in `membership`. Add it back to them with `joinMailingList`.

<div className="[&_table]:w-full [&_table]:table-fixed [&_th:first-child]:w-44">

|                               | [`enableRecipient`](/api/contacts#restore-contact-by-id) | [`reactivateRecipient`](/api/contacts#restore-contact-by-email-sms)      |
| ----------------------------- | -------------------------------------------------------- | ------------------------------------------------------------------------ |
| Finds the contact by          | ID                                                       | Email address or SMS number, in `email`                                  |
| Confirmation                  | Always confirmed                                         | Confirmed with `confirm`, otherwise given a confirmation code            |
| Address blocked after bounces | Removes the block and restores the contact               | Returns an error                                                         |
| Active contact                | Returns an error                                         | Confirms it, or gives it a confirmation code                             |
| Pseudonymized contact         | Returns `0`                                              | Restores it with the same ID, unconfirmed and without its old properties |
| Forgotten contact             | Returns `0`                                              | Returns an error                                                         |

</div>

Use `enableRecipient` to undo a deletion you made yourself, and `reactivateRecipient` when the
person subscribes again. Without `confirm`, a reactivated contact gets no mail from lists until it
is confirmed, for example through a [confirmation email](/api/confirmation#send-confirmation-email).

## Creating or importing a contact again

[`createRecipient`](/api/contacts#create-contact), `createAndJoinRecipient` and the list imports
look for an existing contact with the same email address or SMS number. What they do depends on
its state:

<div className="[&_table]:w-full [&_table]:table-fixed [&_th:first-child]:w-36">

| Existing contact          | `createRecipient`         | `createAndJoinRecipient`                | List import              |
| ------------------------- | ------------------------- | --------------------------------------- | ------------------------ |
| Active                    | Error                     | Updates it and adds it to the lists     | Updated and added        |
| Active, has left the list | Error                     | Updates it and adds it back to the list | Skipped                  |
| Deleted                   | Error                     | Returns `false`, nothing changes        | Skipped                  |
| Pseudonymized             | Restored with its old ID  | Restored with its old ID                | Restored with its old ID |
| Forgotten                 | New contact with a new ID | New contact with a new ID               | Skipped                  |

</div>

Pseudonymized and forgotten contacts are recognized by their email address.

The v1 and v2 imports behave the same way, and also skip addresses that were deleted because mail
to them bounced. In [`getImportLog`](/api/list-imports#get-import-log), skipped contacts that had
left the list or were deleted count as `Unsubscribed`, and bounced and forgotten addresses as
`Deleted`. Truncating the list does not change this, because it removes only active members. See
[Importing contacts](./importing).

Adding a contact to a list it has left, with `joinMailingList` or `createAndJoinRecipient`,
subscribes it again. Do this only when the person has asked for it.

## Finding a contact in any state

| Endpoint                                                            | Finds                         | Returns                                   |
| ------------------------------------------------------------------- | ----------------------------- | ----------------------------------------- |
| `getRecipient`                                                      | An active contact by ID       | An error for other contacts               |
| `getDeletedRecipient`                                               | A deleted contact by ID       | An error for an active contact            |
| [`getShadowRecipient`](/api/contacts#get-active-or-deleted-contact) | Any contact by ID             | The contact, with `enabled`               |
| [`getDeletedRecipients`](/api/contacts#get-deleted-contacts)        | All deleted contacts, paged   | Forgotten and pseudonymized ones included |
| [`isEnabledRecipient`](/api/contacts#check-if-contact-is-active)    | A contact by ID, email or SMS | `1` if it is active                       |
| [`getRecipientsStatusByEmail`](/api/contacts#get-contact-statuses)  | Many email addresses          | `0` active, `1` deleted, `2` no contact   |
| [`existsRecipient`](/api/contacts#find-contact-id)                  | A contact by email or SMS     | Its ID, also of a deleted contact         |

[`getRecipientByEmail`](/api/contacts#get-contact-by-email) returns deleted contacts too if you set
`include_deleted`. Forgotten and pseudonymized contacts can no longer be found by their old email
address or SMS number, only by their ID.

## GDPR requests

<Stepper>

1. **Export the contact's data**

   For a data access request, call
   [`getRecipientDatafile`](/api/contacts#export-contact-data). It returns a Base64-encoded XLS
   file, or a zipped JSON file, with the contact's email address and SMS number, consents,
   received mails, opens, clicks and logged events, and the properties you list by handle in
   `options.properties`. It works for deleted contacts too.

1. **Forget the contact**

   For an erasure request, call [`forgetRecipient`](/api/contacts#forget-contact) with the
   contact's ID, or [`forgetRecipientByEmail`](/api/contacts#forget-contact-by-email) or
   [`forgetRecipientBySMS`](/api/contacts#forget-contact-by-sms). They find deleted contacts too.
   The contact is deleted and leaves its lists. Its properties, SMS number, consents and logged
   events are removed, and its email address is replaced with a hash. You cannot undo this, so
   export the data first if you need it.

</Stepper>
