How do you delete all the cookies for the current domain using JavaScript?
function deleteAllCookies() {
document.cookie.split(';').forEach(cookie => {
const eqPos = cookie.indexOf('=');
const name = eqPos > -1 ? cookie.substring(0, eqPos) : cookie;
document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:00 GMT';
});
}
Note that this code has two limitations:
HttpOnly
flag set, as the HttpOnly
flag disables JavaScript's access to the cookie.Path
value. (This is despite the fact that those cookies will appear in document.cookie
, but you can't delete it without specifying the same Path
value with which it was set.)