|Firefox (3.0, 3.6, 30, 31)|
|Chrome (5, 34, 36)|
|IE (6, 7, 8, 11)|
Suppose that you wish to delete a cookie named
foo. It was created without an explicit
The method described here has three steps:
- Construct an empty cookie expressed as a name-value pair.
- Specify an expiry date for the cookie that is in the past.
- Assign the resulting string to
var cookieString = 'foo='; var expiryDate = new Date(); expiryDate.setTime(expiryDate.getTime() - 86400 * 1000); cookieString += ';max-age=0'; cookieString += ';expires=' + expiryDate.toUTCString(); document.cookie = cookieString;
You may also need to:
- Define the scope of the cookie (path and/or domain).
Note that a cookie created via HTTP with the
When a cookie is created it is expressed as a name-value pair of the form
The same format is needed for deletion, except that any value will suffice. The empty string is the obvious choice:
var cookieString = 'foo=';
The preferred way to specify an expiry date is by means of the
max-age attribute. Normally the argument to this attribute would be a time difference in seconds, but as a special case, an argument of zero is interpreted as the earliest date that the web browser is capable of representing.
For compatibility with Internet Explorer you may also want to append an
expires attribute. (
max-age takes precedence if it is present and supported.) Common practice is to subtract one day from the current date and time:
var expiryDate = new Date(); expiryDate.setTime(expiryDate.getTime() - 86400 * 1000); cookieString += ';max-age=0'; cookieString += ';expires=' + expiryDate.toUTCString();
The cookie string should now be assigned to the
cookie property of the global
document object in the same way that it would be to create a new cookie:
document.cookie = cookieString;
The scope of a cookie consists of an optional path and domain which determine the extent to which it is visible from other web pages. To successfully delete a cookie, the scope specified in the deletion request must match that given when the cookie was created. For example, for a cookie scoped to the root of the domain
cookieString += ';path=/'; cookieString += ';domain=example.com'; document.cookie = cookieString;
Note that a given web page may be able to see several cookies with the same name but different scopes. Therefore, even if deletion is successful, you may still be able to see one or more cookies with the name that was deleted.
- Document.cookie, Web API Interfaces, Mozilla Developer Network
- A. Barth, HTTP State Management Mechanism, RFC 6265, IETF, April 2011
- Persistent Client State HTTP Cookies, Netscape (historical specification)