Skip to content

Instantly share code, notes, and snippets.

@bilelz
Last active January 17, 2023 14:50
Show Gist options
  • Save bilelz/1111274584beac74d7e97785ad59f70f to your computer and use it in GitHub Desktop.
Save bilelz/1111274584beac74d7e97785ad59f70f to your computer and use it in GitHub Desktop.
Clear all cookies in all paths and all variants of the domain (www.mydomain.example, mydomain.example, etc...)
function clearCookies() {
/* Clear all cookies in all paths and all variants of the domain (www.mydomain.example, mydomain.example, etc...) */
const cookies = document.cookie.split("; ");
for (let cookie of cookies) {
const domains = window.location.hostname.split(".");
while (domains.length > 0) {
// prettier-ignore
const cookieName = encodeURIComponent(cookie.split(";")[0].split("=")[0]);
const cookieBase = `${cookieName}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; domain=${domains.join(".")} ;path=`; // prettier-ignore
const paths = location.pathname.split("/");
document.cookie = cookieBase + "/";
while (paths.length > 0) {
document.cookie = cookieBase + paths.join("/");
paths.pop();
}
domains.shift();
}
}
/* Thanks to https://stackoverflow.com/a/33366171/1977459 */
@bilelz
Copy link
Author

bilelz commented Jan 17, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment