stacknist
stacknist

Reputation: 587

delete cookie key value pair

I am trying to delete a cookie by setting the expiry date time as follows. However the key of the cookie still exists. This only sets the cookie to the empty string. How to remove the key value pair of the cookie completely?

document.cookie = "username=; expires=Thu, 01 Jan 1970 00:00:00 UTC";

Upvotes: 1

Views: 2575

Answers (1)

Vikram Deshmukh
Vikram Deshmukh

Reputation: 15706

AFAIK deleting cookies set by the server can be tricky. In order to delete cookies set using JavaScript, use the following method:

function deleteCookie(c_name) {
    document.cookie = c_name+'=' + ";expires=Thu, 01 Jan 1970 00:00:01 GMT ;domain=.yourdomain.com;path=/";
    document.cookie = c_name+'=' + ";expires=Thu, 01 Jan 1970 00:00:01 GMT ;";
}

Just call this method as: deleteCookie("username");

Upvotes: 3

Related Questions