How to delete cookie by name with JavaScript?

Sometimes, we want to delete cookie by name with JavaScript.

In this article, we’ll look at how to delete cookie by name with JavaScript.

How to delete cookie by name with JavaScript?

To delete cookie by name with JavaScript, we set document.cookie to a string that has the name of the cookie followed by the expiry date.

For instance, we write

const deleteCookie = (name) => {
  document.cookie = name + "=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;";
};

to add 'Expires=Thu, 01 Jan 1970 00:00:01 GMT;' after name to set the expiry date of the cookie with name to the past to remove it in the deleteCookie function.

Conclusion

To delete cookie by name with JavaScript, we set document.cookie to a string that has the name of the cookie followed by the expiry date.