1

I have a cookie

console.log(document.cookie);
Clickme=a-6,a-7,a-8,a-9,a-10,a-17,a-8,

I want to delete this cookie, I have tried following things but they aren't working

document.cookie = "Clickme=; max-age = -1;"
document.cookie =  Clickme+"=; expires=Thu, 01 Jan 1970 00:00:01 GMT;"
document.cookie = "Clickme=; expires=Thu, 01 Jan 1970 00:00:00 UTC";
2
  • Possible duplicate of javascript - delete cookie Commented Sep 2, 2016 at 14:46
  • the second attempt should've worked - if it were correct javascript (so close) document.cookie = "Clickme=; expires=Thu, 01 Jan 1970 00:00:01 GMT" - third attempt should've worked too Commented Sep 2, 2016 at 14:55

3 Answers 3

1

I use the following to remove a cookie.

function deleteCookie(name) {
  var domain = location.hostname,
      path = '/'; // root path

  document.cookie = [
    name, '=',
    '; expires=' + new Date(0).toUTCString(),
    '; path=' + path,
    '; domain=' + domain
  ].join('');
}

deleteCookie('Clickme');

Using an array helps me see the parts separately, and using Date(0).toUTCString() guarantees I've got the correct date.

Sign up to request clarification or add additional context in comments.

1 Comment

This does require that the cookie path was set to the root. In hindsight it might have been better to pass path and domain through as arguments.
0

Try this. function delete_cookie( name ) { document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;'; }

Hope it works.

Comments

0

See this Delete cookie

You can use this function

function removeItem(sKey, sPath, sDomain) {
    document.cookie = encodeURIComponent(sKey) + 
                  "=; expires=Thu, 01 Jan 1970 00:00:00 GMT" + 
                  (sDomain ? "; domain=" + sDomain : "") + 
                  (sPath ? "; path=" + sPath : "");
}

removeItem("cookieName");

In angular you can use $cookies.remove as well, if that helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.