How to Delete a Cookie in JavaScript?

Estimated read time 1 min read

In JavaScript, you can delete a cookie by setting its expiration date to a date in the past. Here’s an example:

// Delete a cookie by name
function deleteCookie(name) {
  document.cookie = name + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";

In this example, we’re defining a function called deleteCookie that takes a single parameter, which is the name of the cookie to delete. The function sets the value of the cookie to an empty string and sets the expires property to a date in the past to immediately expire the cookie. We also set the path property of the cookie to / to make sure the cookie is deleted for all paths within the domain.

To use this function, you can call it like this:


This will delete the myCookie cookie. Note that this function assumes that the cookie is set with the path property set to /. If the cookie was set with a different path, you will need to update the path property in the function accordingly.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply