'Unable to delete cookie from javascript

I am on an external site, and I am trying to delete the cookie via javascript.

I did the following in the console:

function deleteAllCookies() {
    var cookies = document.cookie.split(";");

    for (var i = 0; i < cookies.length; i++) {
        var cookie = cookies[i];
        var eqPos = cookie.indexOf("=");
        var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
        document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
    }
}

deleteAllCookies()

which is supposed to set the document cookie to expire in 1970

But after that, I call

document.cookie.split(";")

The cookies are seemingly untouched. Any ideas why?

PS: code above is from stackoverflow Clearing all cookies with JavaScript



Solution 1:[1]

Your cookie is most likely not being deleted because when you set the new value, it has to match the path and domain of the original cookie you're trying to delete.

In other words:

 document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=[something];"

that "something" value needs to line up with whatever the existing cookies have set.

JS debuggers might not give you details on what the path and domain are, but it will become obvious which one you're not matching on if you look up the value of the existing cookie in your Chrome->settings or similar panel in Firefox/Safari/IE.

Let me know if that helps.

Solution 2:[2]

I had the same issue. I discovered that the cookie was set under an empty subdomain, e.g. the cookie domain was ".domain.com", and my website was hosted at "sub.domain.com".

To fix I added the cookie domain to the set value

document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT; domain=.domain.com";

To see what domain the cookie is set to, in Chrome, open dev tools -> resources -> cookies and look at the domain fields.

Solution 3:[3]

I had a similar issue when trying to remove certain cookies. Sometimes this worked:

document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:01 GMT;path=/;';

...and sometimes it didn't.

After looking into the Chrome Inspector (Application tab -> Storage sidebar -> Cookies) I noticed that some cookies were set with different domains. Example:

.mydoamin.com
sub.mydomain.com 

So, my solution was to make a generic function that removes the cookie from all domains.

var deleteCookie = function(name) {
    document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:01 GMT;path=/;domain=.mydomain.com;';
    document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:01 GMT;path=/;domain=sub.mydomain.com;';
};

Solution 4:[4]

For me the issue was that I was setting the domain field which is required only if you overrode it when setting the cookie. Therefore, the following should do the trick:

document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/"

Solution 5:[5]

Clear session cookies in ie11?

May be The Link above can give a Help

Just run the JavaScript like Below

document.execCommand("ClearAuthenticationCache")

I tried and The cookie was cleared.

Solution 6:[6]

What helped me was that you need to delete the cookie in the same state as you created it.

If you created it with a path / and a domain mydomain.com, then you have to delete it with those.

If you did not use path or domain when creating the cookie, you can not have the path or domain when deleting it.

Trick was to delete it in the EXACT same way it was created.

Hope this helps more people, I went crazy on this one even though I used the right path and domain..

Solution 7:[7]

I was working on a browser bookmarklet to remove cookies from the current domain, I had the same issue, my issue was that I was not using domain either. Here is my bookmarklet value eventually:

javascript: (function(){document.cookie.split(";").forEach(function(c) { document.cookie = c.replace(/^ +/, "").replace(/=.*/, "=;expires=" + new Date().toUTCString() + ";domain=." + location.host.split('.').slice(-2).join(".") +";path=/"); }); })();

Note that I replace "domain.com" with location.host.split('.').slice(-2).join(".") so that I always get the domain name without subdemains, i.e. mail.google.com would become google.com. when setting cookie expiry we should ignore the subdemain (at least in my case it was the case.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 isherwood
Solution 2
Solution 3 Ponyboy
Solution 4 Nicholas K
Solution 5 Bin Liu
Solution 6 Michael Cavallin
Solution 7 Mohammad Baygi