Reference · Cookies and sessions

delete_cookie

signature
delete_cookie(str, map | null=) -> null

What it does

Tells the browser to forget a cookie: a Set-Cookie for the name with an empty value and Max-Age=0 joins the request's answer, whatever the route returns. The options are path and domain, which must be the ones the cookie was set with, or the browser keeps it, and http_only, secure and same_site, as set_cookie takes them; max_age only as 0, and another option is an error. It gives null. See Headers, cookies and sessions.

Examples

The routes are posted to by a button on a page, a form of its own: <form method="post" action="/theme/reset"><button>Back to the default</button></form>. Its answer carries set-cookie: theme=; Path=/; Max-Age=0, and the page it redirects to reads cookie("theme") as null.

in a program
route POST /theme/reset { delete_cookie("theme"); redirect("/") }route POST /forget {    db.exec("delete from remember where token = ?", cookie("remember"))    delete_cookie("remember", { path: "/" })    redirect("/")}
print(delete_cookie("theme"))
output
null

Edge cases

print(delete_cookie("theme", { path: "/", domain: "example.com" }))print(delete_cookie("missing"))
output
nullnull

Errors

A bad cookie name, no name, a path not starting with /, a domain that is not a host name, an option it does not take (a max_age other than 0 too), and options that are not a map are runtime errors, not error values.

this one fails
delete_cookie()
output
line 1: `delete_cookie` needs a cookie name
this one fails
delete_cookie("theme", { path: "theme" })
output
line 1: `delete_cookie`'s path starts with /, got "theme"
this one fails
delete_cookie("theme", { max_age: 5 })
output
line 1: `delete_cookie` has no option `max_age` but 0: a deleted cookie's age is 0

Chaining

It is a statement before the route's answer, not a pipeline stage. To end a visit fully, delete the cookies and end the session together:

in a program
route POST /logout {    delete_cookie("remember")    end_session()    redirect("/")}

See also

set_cookie, cookie, end_session

Thread
7Useful? Like it, or add to its thread below.

Thread 0 posts

On delete_cookie(): questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.

    Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo