How to clear all cookies with JavaScript?

The cookies are the text or data stored in the format of text files in the browser or user’s computer, and it is one kind of cache memory.

The developer can store the username, authentication tokens, etc., in the browser’s cookies. When a user visits the webpage for the first time, it retrieves the user information from the server and stores it in the cookie format in the browser. After that, when a user visits the webpage again, it retrieves the required data from cookies instead of the server, making the application fast and secure.

Every browser has a limit on storing cookies. The most modern browsers allow users to store cookies of the maximum size of 4kb. However, the different browser allows to store of a different number of cookies. Google chrome allows 180, and Firefox allows 150, etc.

Clear all Cookies using JavaScript

This section will look at how we can clear all cookies using JavaScript. Every cookie contains the expiry attribute, which contains the expiry date and time for the particular cookies. We can retrieve all cookies and set the past expiry date to clear all cookies.

Syntax

Users can follow the syntax below to set the past expiry date to all cookies.

// retrieve all cookies
var Cookies = document.cookie.split(';');
 // set past expiry to all cookies
for (var i = 0; i < Cookies.length; i++) {
   document.cookie = Cookies[i] + "=; expires="+ new Date(0).toUTCString();
}

Algorithm

Users can follow the algorithm below to clear all cookies.

  • Step 1 − Get all cookies using document.cookies.
  • Step 2 − Split the all cookies with delimiter ‘;’, and it returns the array of cookies.
  • Step 3 − Iterate through every cookies and set value of ‘expires’ attribute to the past expiry date.

Example

In the example below, we have created two button names, ‘show cookies’ and ‘clear cookies.’ When a user clicks on any of them, it will call the respective function to display cookies or erase cookies. We will set the object in the cookies when the webpage loads.

<html>
<head>
</head>
<body>
   <h2>Clear all cookies using JavaScript</h2>
   <h4>Click on the below buttons to show and clear cookies.</h4>
   <button onclick = "showCookies()">show Cookies</button>
   <button onclick = "deleteCookies()">clear Cookies</button>
   <div id = "show"></div>
   <script type ="text/javascript">
      
      // function to show cookies
      function showCookies() {
         var show = document.getElementById("show");
         show.innerHTML = document.cookie;
      }

      // function to delete cookies
      function deleteCookies() {
         var Cookies = document.cookie.split(';');
 
         // set 1 Jan, 1970 expiry for every cookies
         for (var i = 0; i < Cookies.length; i++)
         document.cookie = Cookies[i] + "=;expires=" + new Date(0).toUTCString();
         showCookies();
      }
 
      // set object in the cookies on webpage load.
      window.onload = () => {
         let object = { name: "FirstEnquiry", site: "firstenquiry.com", }
         document.cookie = 'object=' + JSON.stringify(object);
      }
   </script>
</body>
</html>

In the above output, users can see the object at last in the cookies when they click on the Show cookies button. When a user clicks on the clear cookies button, it will remove objects from the cookies. Here, we have cleared all cookies which we set up.

Related Posts

What are custom events in JavaScript?

Custom events are the events that allow you to decouple the code you want to run after a specific piece of code runs. There are various in-built events…

How to use nested for loop in JavaScript?

We use the for loop statement of JavaScript for repeating a set of statements inside the loop body a specified number of times. A nested for loop, as the…

What are the basic rules for JavaScript parameters?

A JavaScript function is a code that performs a particular task. The function parameters are the name list in the function definition. Parameters are also known as…

How to stop refreshing the page on submit in JavaScript?

Using event.preventDefault() to stop page refresh on form submit In this section, we will see how to use event.preventDefault() to stop page refresh on form submission. The event.preventDefault() restricts the default…

Target a Window Using JavaScript or HTML

TARGET attribute of HTML A link’s opening named frame or window is specified using the Target attribute of the <a> anchor tag. The concluding </a> tag in…

What is the role of deferred scripts in JavaScript?

Since JavaScript is a loosely typed language, you are not required to correctly predict the kind of data that will be kept in a variable. Depending on the information…