How can I delete all cookies with JavaScript?

JavascriptCookies

Javascript Problem Overview


I have written code to save cookies in JavaScript. Now I need to clear the cookies irrespective of values that I assigned.

Are there any script modules to delete all cookies that were generated by Javascript?

My Sample Code:
document.cookie = 'ppkcookie2=another test; expires=Fri, 3 Aug 2001 20:47:11 UTC; path=/'

function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}

How else could I clear all of the cookies?

Will there will be any problems when I test the code on the webserver?

Javascript Solutions


Solution 1 - Javascript

There is no 100% solution to delete browser cookies.

The problem is that cookies are uniquely identified by not just by their key "name" but also their "domain" and "path".

Without knowing the "domain" and "path" of a cookie, you cannot reliably delete it. This information is not available through JavaScript's document.cookie. It's not available through the HTTP Cookie header either!

However, if you know the name, path and domain of a cookie, then you can clear it by setting an empty cookie with an expiry date in the past, for example:

function clearCookie(name, domain, path){
    var domain = domain || document.domain;
    var path = path || "/";
    document.cookie = name + "=; expires=" + +new Date + "; domain=" + domain + "; path=" + path;
};

Solution 2 - Javascript

On the face of it, it looks okay - if you call eraseCookie() on each cookie that is read from document.cookie, then all of your cookies will be gone.

Try this:

var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++)
  eraseCookie(cookies[i].split("=")[0]);

All of this with the following caveat:

  • JavaScript cannot remove cookies that have the HttpOnly flag set.

Solution 3 - Javascript

This is a function we are using in our application and it is working fine.

delete cookie: No argument method

function clearListCookies()
{	
	var cookies = document.cookie.split(";");
	for (var i = 0; i < cookies.length; i++)
	{	
	  	var spcook =  cookies[i].split("=");
		deleteCookie(spcook[0]);
	}
	function deleteCookie(cookiename)
	{
        var d = new Date();
        d.setDate(d.getDate() - 1);
    	var expires = ";expires="+d;
		var name=cookiename;
		//alert(name);
		var value="";
		document.cookie = name + "=" + value + expires + "; path=/acc/html";					
	}
	window.location = ""; // TO REFRESH THE PAGE
}

Edit: This will delete the cookie by setting it to yesterday's date.

Solution 4 - Javascript

Why do you use new Date instead of a static UTC string?

    function clearListCookies(){
    var cookies = document.cookie.split(";");
        for (var i = 0; i < cookies.length; i++){   
            var spcook =  cookies[i].split("=");
            document.cookie = spcook[0] + "=;expires=Thu, 21 Sep 1979 00:00:01 UTC;";                                
        }
    }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionvenkatachalamView Question on Stackoverflow
Solution 1 - Javascriptjb.View Answer on Stackoverflow
Solution 2 - JavascriptGussView Answer on Stackoverflow
Solution 3 - JavascriptantnewbeeView Answer on Stackoverflow
Solution 4 - JavascriptpykissView Answer on Stackoverflow