Delete Cookies with JavaScript | Plantpot

Delete Cookies with JavaScript

Web Dev
Sponsored links

In this article, I will introduce how to delete cookies with JavaScript.

Delete cookies with JavaScript

There are several ways to delete cookies, but you can also delete them with JavaScript.
A cookie has max-age (the key with the remaining time of the cookie) and expires (the key with the date to destroy the cookie).

You can delete cookies by setting the max-age value to 0 or by setting expires to a date in the past.

In JavaScript, operate the cookie as follows:

document.cookie = "key=value;key2=value2"

If you want to enter values ​​continuously, connect them with ;.

Sponsored links

Sample Code

Below is sample code to delete cookies in two ways.

// Set cookies
document.cookie = "key1=value1"
document.cookie = "key2=value2"


// Delete a cookie by setting the max-age value to 0
document.cookie = "key1=;max-age=0"


// Delete a cookie by setting expires to a date in the past
var dt = new Date('2010-01-01T23:59:59Z');
document.cookie = `key2=;expires=${dt.toUTCString()}`;


console.log(document.cookie)