|
/** |
|
* |
|
* 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.`); |
|
})(); |