Cover Image for Deleting Cookies
114 views

Deleting Cookies

To delete a cookie in JavaScript, you need to set its expiration date in the past. Here’s an example of how to delete a cookie:

JavaScript
document.cookie = "cookieName=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";

In this example, cookieName represents the name of the cookie you want to delete. By setting the expiration date to a time in the past (January 1, 1970, in this case), the cookie will immediately expire and be removed from the browser’s storage.

Note that when deleting a cookie, it’s important to provide the same path and domain that were used when the cookie was initially set. Otherwise, the deletion may not work correctly.

If you want to delete multiple cookies, you’ll need to repeat the process for each cookie. For example:

JavaScript
document.cookie = "cookie1=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
document.cookie = "cookie2=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
document.cookie = "cookie3=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";

In this case, cookie1, cookie2, and cookie3 represent the names of the cookies you want to delete.

Make sure to adjust the names and attributes of the cookies according to your specific use case.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS