How to Delete a Cookie?

Jose C houdinihound69 at
Thu Jan 8 19:16:28 CET 2009

> c["mycook"]["expires"] = 0

Set ["expires"] using the following format to any time less than
current (which causes the browser to delete the cookie).
Here's a function I use to return a cookie expiry timestamp, negative
values passed in result in cookie being deleted.

def cookie_expiry_date(numdays):
    """ Returns a cookie expiry date in the required format.  -ve
value in = kill cookie.
    `expires` should be a string in the format "Wdy, DD-Mon-YY
    NOTE!  Must use [expires] because earlier IE versions don't
support [max-age].
    from datetime import date, timedelta
    new = + timedelta(days = numdays)
    return new.strftime("%a, %d-%b-%Y 23:59:59 GMT")

c["mycook"]["expires"] = cookie_expiry_date(-10)  # any negative value
will remove cookie


More information about the Python-list mailing list