Skip to content

Instantly share code, notes, and snippets.

@tylerzey
Created August 29, 2025 14:53
Show Gist options
  • Select an option

  • Save tylerzey/36b95766213a65782496e4b7bcbae1ea to your computer and use it in GitHub Desktop.

Select an option

Save tylerzey/36b95766213a65782496e4b7bcbae1ea to your computer and use it in GitHub Desktop.
JavaScript Snippet for Deleting Cookies
/**
*
* WARNING / DISCLAIMER:
* ----------------------
* This snippet is intended solely as guidance for implementing cookie deletion.
* It does NOT constitute a recommendation in any form.
* Consider the compliance, privacy, and UX implications for your environment
* before using it, and ensure transparency in your data handling practices.
*
* This is not functionality that any vendor tool provides.
* The following code is for example purposes only.
* Support teams cannot assist with implementing this functionality.
*
* HOW IT WORKS:
* - You define a whitelist of cookies that should NOT be deleted.
* - All other cookies will be deleted from the current domain and subdomains.
*/
(function deleteAllCookies() {
// ✏️ Customize this list of cookies you want to KEEP
const whitelist = ["session_id", "csrftoken"];
const cookies = document.cookie.split(';');
const domainParts = location.hostname.split('.');
cookies.forEach(cookieStr => {
const cookieName = cookieStr.split('=')[0].trim();
// Skip if cookie is whitelisted
if (whitelist.includes(cookieName)) {
console.log(`[SKIP] ${cookieName} is whitelisted`);
return;
}
// Delete with multiple strategies
document.cookie = `${cookieName}=; Max-Age=0; path=/;`;
document.cookie = `${cookieName}=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/;`;
// Try deleting across domain permutations
let parts = [...domainParts];
while (parts.length > 0) {
const domain = '.' + parts.join('.');
document.cookie = `${cookieName}=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/; domain=${domain};`;
parts.shift();
}
console.log(`[DELETE] ${cookieName}`);
});
console.log(`[deleteAllCookies] Processed ${cookies.length} cookies.`);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment