Remove all cookies from the current website using Javascript

Cookies are small pieces of data that are stored on a user's computer when they visit a website. Cookies can be used to store information about a user's preferences or to track a user's activity on a website.

function clear_all_cookies() {
    var all_cookies = document.cookie.split(";");

    for (var i = 0; i < all_cookies.length; i++) {
        var single_cookie = all_cookies[i];
        var cookie_index = single_cookie.indexOf("=");
        var cookie_name = cookie_index > -1 ? all_cookies.substr(0, cookie_index) : single_cookie;
        document.cookie = cookie_name + "=;expires=" + new Date().toUTCString();

In order to clear all cookies from a website using Javascript, you can use the code examples explained in this post. In the above code example, we have created a function - clear_all_cookies() that will remove all the cookies from the current website when executed.

Another code example is as below that will use the forEach() function of Javascript to loop through all the cookies and removes them one by one.

First, open up your javascript console. Then, copy and paste the following code: 

document.cookie.split(";").forEach(function (cookie) { 
    document.cookie = cookie.replace(/^ +/, "").replace(/=.*/, "=;expires=" + new Date().toUTCString() + ";path=/"); 

That's it! Now all of the cookies from the current website should be cleared.

Was this helpful?