Deleting a Cookie in JavaScript

In the previous section, we learned the different ways to set and update a cookie in JavaScript. Apart from that, JavaScript also allows us to delete a cookie. Here, we see all the possible ways to delete a cookie.

Different ways to delete a Cookie

These are the following ways to delete a cookie:

  • A cookie can be deleted by using expire attribute.
  • A cookie can also be deleted by using max-age attribute.
  • We can delete a cookie explicitly, by using a web browser.

Examples to delete a Cookie

Example 1

In this example, we use expire attribute to delete a cookie by providing expiry date (i.e. any past date) to it.

Example 2

In this example, we use max-age attribute to delete a cookie by providing zero or negative number (that represents seconds) to it.

Example 3

Let's see an example to set, get and delete multiple cookies.

Example 4

Let's see an example to delete a cookie explicitly.

After clicking Set Cookie once, whenever we click Get Cookie, the cookies key and value is displayed on the screen.

JavaScript Deleting a Cookie

To delete a cookie explicitly, follow the following steps:

  • Open Mozilla Firefox.
  • Click Open menu - Library - History - Clear Recent History - Details.
JavaScript Deleting a Cookie
  • Here we can see a Cookies checkbox which is already marked. Now, click Clear Now to delete the cookies explicitly.

Now, on clicking Get Cookie, the below dialog box appears.

JavaScript Deleting a Cookie

Here, we can see that the cookies are deleted.

Next TopicJavaScript Events




Contact US

Email:[email protected]

Deleting Cookies
10/30