res.clearCookie()
Clears a cookie by setting it to expire immediately.
Syntax
res.clearCookie(name[, options]) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
name | string | Yes | The cookie name to clear. |
options | object | No | Must match the `path`/`domain`/`secure`/`sameSite` used when the cookie was set. |
Returns
Response — The response object.
Examples
app.post('/logout', (req, res) => {
res.clearCookie('sid');
res.sendStatus(204);
});
Output
Set-Cookie: sid=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT
app.post('/logout', (req, res) => {
res.clearCookie('sid', { path: '/app', httpOnly: true });
res.redirect('/');
});
Notes
The options (especially `path` and `domain`) must match those used
in `res.cookie`, or the browser keeps the original cookie. Clearing
is done by sending an expired `Set-Cookie`; there is no real
server-side deletion.