Clear all cookies from a website?

Guys please tell me how to create a button than when clicked will remove all the cookies from that domain or at least the cookie that has some specific name???

I have seen many codes but without the button!

Really need your help here!

<button onclick="document.cookie=&quot;firstname=;expires=Wed; 01 Jan 1970&quot;" type="button">Delete Cookie 1</button>

There is no guaranteed way to delete all cookies using from JavaScript, because they are identified not only by their name, but by the subdomain and path too.

The following jQuery code will delete all cookies that are available from the subdomain and path that you are on:

var cookies = $.cookie();
for(var cookie in cookies) {
   $.removeCookie(cookie);
}

As for non-library JavaScript code (typically called vanilla JavaScript), you could use:

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
}

Just don’t promise to be able to delete all cookies. You cannot guarantee that unless you go to the web browsers profile folder and delete them all from there manually.

Thanks buddy so much but where is the HTML button? I wanna add a button and let users click on it if they decide to?

<button type="button" id="clearCookies">I'm a button!</button>

And you connect the button to the function with:

document.getElementById("clearCookies").onclick = clearListCookies;

Awesome thanks so much that is it.

A good one, needed for quite sometime!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.