<p>Okay, here is the 411 - I have the following event handler in my Global.asax.cs file:</p>

<pre><code>private void Global_PostRequestHandlerExecute(object sender, EventArgs e)
{
   if (/* logic that determines that this is an ajax call */)
   {
      // we want to set a cookie
      Response.Cookies.Add(new HttpCookie("MyCookie", "true"));
   }
}
</code></pre>

<p>That handler will run during Ajax requests (as a result of the Ajax framework I am using), as well as at other times - the condition of the if statement filters out non-Ajax events, and works just fine (it isn't relevant here, so I didn't include it for brevity's sake).</p>

<p>It suffices us to say that this works just fine - the cookie is set, I am able to read it on the client, and all is well up to that point.</p>

<p>Now for the part that drives me nuts.</p>

<p>Here is the JavaScript function I am using to delete the cookie:</p>

<pre><code>function deleteCookie(name) {
   var cookieDate = new Date();
   cookieDate.setTime(cookieDate.getTime() - 1);
   document.cookie = (name + "=; expires=" + cookieDate.toGMTString());
}
</code></pre>

<p>So, of course, at some point after the cookie is set, I delete it like so:</p>

<pre><code>deleteCookie("MyCookie");
</code></pre>

<p>Only, that doesn't do the job; the cookie still exists. So, anyone know why?</p>

[{'answer_id': 56261, 'author': 'Erlend', 'author_id': 5746, 'author_profile': 'https://Stackoverflow.com/users/5746', 'pm_score': 2, 'selected': False, 'text': '<p>Have you tried to use <code>;expires=Thu, 01-Jan-1970 00:00:01 GMT</code>?</p>\n'}, {'answer_id': 56287, 'author': 'Rob Cooper', 'author_id': 832, 'author_profile': 'https://Stackoverflow.com/users/832', 'pm_score': 2, 'selected': False, 'text': '<p>Weird.. The code you pasted is almost verbatim to this: <a href="http://www.quirksmode.org/js/cookies.html" rel="nofollow noreferrer">http://www.quirksmode.org/js/cookies.html</a> which works fine..</p>\n<p>I know you are using Ajax, but have you tried quickly <strong>knocking it to server side code to see if that works</strong>? This may help in figuring if it is a problem with the JS or something else (e.g mystery file locking on the cookie)?</p>\n<h3>Update</h3>\n<p>Just had a quick Google, looks like there may be issues with browser settings as well. I don\'t think your problem is the code here, it\'s more likely to be something else. I would suggest try the above as PoC and we can move from there. :)</p>\n'}, {'answer_id': 56740, 'author': 'Ricky', 'author_id': 653, 'author_profile': 'https://Stackoverflow.com/users/653', 'pm_score': 2, 'selected': False, 'text': '<p>I posted a js cookie util a week or so ago on <a href="http://rickyrosario.com/blog/javascript-cookie-utility" rel="nofollow noreferrer">my blog</a>. This has worked for me on all "A Grade" browsers.</p>\n\n<pre><code>var CookieUtil = {\n  createCookie:function(name,value,days) {\n    if (days) {\n        var date = new Date();\n        date.setTime(date.getTime()+(days*24*60*60*1000));\n        var expires = "; expires="+date.toGMTString();\n    }\n    else var expires = "";\n    document.cookie = name+"="+value+expires+"; path=/";\n  },\n  readCookie:function(name) {\n    var nameEQ = name + "=";\n    var ca = document.cookie.split(\';\');\n    for(var i=0;i &lt; ca.length;i++) {\n        var c = ca[i];\n        while (c.charAt(0)==\' \') c = c.substring(1,c.length);\n        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);\n    }\n    return null;\n  },\n  eraseCookie:function(name) {\n    createCookie(name,"",-1);\n  }\n};\n</code></pre>\n'}, {'answer_id': 56993, 'author': 'Robert J. Walker', 'author_id': 4287, 'author_profile': 'https://Stackoverflow.com/users/4287', 'pm_score': 2, 'selected': False, 'text': "<ul>\n<li>Have you checked the client-side and server-side cookie domains and paths to ensure they're the same?</li>\n<li>Is one cookie secure and the other not?</li>\n<li>Other than that, I would suspect server/client clock sync issues, as Erlend suggests.</li>\n</ul>\n"}, {'answer_id': 60379, 'author': 'Tyler', 'author_id': 5642, 'author_profile': 'https://Stackoverflow.com/users/5642', 'pm_score': 0, 'selected': False, 'text': "<p>Are we sure there's no code that sets the Cookie to HttpOnly (we're not missing anything above)? The HttpOnly property will stop (modern) browsers from modifying the cookie. I'd be interested to see if you can kill it server side like Rob suggests.</p>\n"}, {'answer_id': 163262, 'author': 'benc', 'author_id': 2910, 'author_profile': 'https://Stackoverflow.com/users/2910', 'pm_score': 0, 'selected': False, 'text': '<p>I assume you are calling this javascript on the browser side. Which browser are you using, how are you viewing the cookie to confirm it is still there? </p>\n'}, {'answer_id': 163307, 'author': 'Andreas Petersson', 'author_id': 16542, 'author_profile': 'https://Stackoverflow.com/users/16542', 'pm_score': 3, 'selected': False, 'text': '<p>you have to delete your cookie at the same path where you created it.\nso create your cookie with path=/ and delte it with path=/ as well..</p>\n'}, {'answer_id': 3714058, 'author': 'just.jimmy', 'author_id': 435635, 'author_profile': 'https://Stackoverflow.com/users/435635', 'pm_score': 1, 'selected': False, 'text': "<p>Also if a cookie domain was specified during the creation, I've found that you must also specify the cookie domain while trying to delete (expire) it.</p>\n"}]
['https://Stackoverflow.com/questions/55860', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1790/']