# Delete first-party browser cookies programmatically

Didomi has developed a script that your organization can utilize on-page to target and delete first-party browser cookies.This script ensures that previously accepted non-essential first-party cookies are no longer used after an end-user refusal of all purposes.&#x20;

* [Identify essential cookies to preserve](#identify-essential-cookies-to-preserve)
* [Implement Didomi programmatic first-party browser cookies deletion script](#implement-didomi-programmatic-first-party-browser-cookies-deletion-script)
* [Third-party trackers](#third-party-trackers)

{% hint style="warning" %}
**Note**: The details described below only apply to first-party cookies. Technically, it is not possible to delete third-party cookies that have already been dropped on an end-user's browser if the end-user refuses all purposes later.
{% endhint %}

***

### Identify essential cookies to preserve

The Didomi script allows your organization to specify any cookies you want to preserve so that they are not deleted when the script is executed. Any cookies not specified (as well as the items stored in localStorage) will be deleted.

{% hint style="warning" %}
**Note:** Didomi strongly recommends that you include the following Didomi cookies in your list of preserved cookies:

* `didomi_dcs` - The Didomi Consent String (DCS) is a  binary format that replaced didomi\_token and offers a smaller, more compact representation of user consent data.
* `didomi_token` - A legacy cookie that contains consent information for custom purposes and vendors, as well as Didomi-specific information.
* `euconsent-v2` - Contains the IAB TCF consent string and consent information for all the standard IAB vendors and purposes.

Preserving these cookies will keep your consent notice from reappearing to re-collect the end-user's consent.
{% endhint %}

When configuring the Didomi script your organization can directly name the cookies that should be preserved and/or use regular expression (regex) to preserve cookies that adhere to a specific pattern.

{% tabs %}
{% tab title="Named cookies" %}
If your organization knows the name of cookies you wish to preserve include those in the `itemsToKeep` array:

```
var itemsToKeep = ["euconsent-v2", "didomi_token", "didomi_dcs"]; 
```

{% endtab %}

{% tab title="Regex" %}
To preserve cookies that adhere to a specific text pattern, your organization can write a regular expression and assign to the `itemsToKeepRegex` variable:

```
var itemsToKeepRegex = /some_regex_[a-z0-9]*/;
```

{% endtab %}
{% endtabs %}

### Implement Didomi programmatic first-party browser cookies deletion script

{% hint style="info" %}
The Didomi provided script is a template that may not directly be applicable to your organization's specific use case and may require adaptation on your behalf to work as intended.
{% endhint %}

The script to delete first-party browser cookies can be found at the link below:

> [Didomi programmatic first-party browser cookies deletion script](https://github.com/didomi/samples/tree/main/how-to-delete-browser-cookies)

The script works on all browsers and is triggered when an end-user who had previously agreed to all vendors and purposes subsequently changes their privacy choices to refuse all vendors and purposes. When the end-user withdraws their consent, the page will automatically reload and all of the cookies that have not been preserved in the script will be deleted from the browser.

### Third-party trackers

Third-party trackers can only be removed by contacting the respective vendor(s) and request their assistance in the removal.

Your organization can use data from your [Compliance Report](broken://pages/2ULkptIS04nbjnSO9Olc) to provide the vendor(s) with a detailed list of the third-party trackers detected on your website. Include the following information:

* **Tracker name**
* **Tracker domain**
* **Tracker type** (e.g., cookie, pixel, etc.)
* **Third-party vendor responsible**

Sharing this information ensures vendors can take appropriate action efficiently.

{% hint style="info" %}
If the trackers are integrated via embedded content, such as videos or podcasts, consider removing the embedded content from your website to eliminate the trackers directly.
{% endhint %}

&#x20;


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.didomi.io/get-started/general/delete-first-party-browser-cookies-programmatically.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
