Express.js res.clearCookie() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Response

Definition

In Express.js, res.clearCookie() tells the browser to drop a cookie by sending a Set-Cookie header with an expiration date in the past. The cookie is cleared on the client after that response — the Node process does not erase the browser’s cookie jar directly.

Pass the same identifying options you used with res.cookie() (especially path and domain). Options such as expires and maxAge are ignored on clear. See the official cookie options for the full list.

Syntax

Call res.clearCookie in your Express.js app. Start with this line.

Express
res.clearCookie(name [, options])
res.cookie('name', 'tobi', { path: '/admin' });
res.clearCookie('name', { path: '/admin' });
CallWhat it does
res.clearCookie(name [, options])Primary signature.
res.cookie('name', 'tobi', { path: '/admin' });Related call form.
res.clearCookie('name', { path: '/admin' });Related call form.

Install with npm install express. Create the app with const app = express() before you use this API in an Express.js tutorial project.

Library declarations
Express
const express = require("express");
const app = express();

res.clearCookie(name [, options])
res.cookie('name', 'tobi', { path: '/admin' });
res.clearCookie('name', { path: '/admin' });

Parameters

ParameterDescription
nameRequired. Cookie name to clear (must match the name used in res.cookie).
optionsOptional. Cookie attributes that should match the original cookie (for example path, domain, secure, sameSite). Do not rely on expires or maxAge — they are ignored.

Return Value

res.clearCookie() returns res for chaining. It instructs the browser to delete a cookie by sending an expired Set-Cookie.

Express
res.clearCookie('session', { path: '/' })
  .redirect('/login');

Express.js Examples

Example 1 — Clear a Cookie on Logout

Expire a default-path cookie and return JSON.

Express
const express = require('express');
const app = express();

app.post('/logout', (req, res) => {
  res.clearCookie('token');
  res.json({ ok: true });
});

app.listen(3000);

How It Works

  1. Name the cookie. clearCookie('token') targets the token cookie.
  2. Past expiry is sent. Express writes a Set-Cookie that expires immediately.
  3. Default path is /. This matches cookies set without a custom path.
  4. Then respond. res.json finishes the response after the clear header is queued.

Example 2 — Clear with Matching Options

Mirror path, domain, and other attributes from when the cookie was set.

Express
const express = require('express');
const app = express();

// Earlier (login):
// res.cookie('adminToken', value, {
//   path: '/admin',
//   domain: 'example.com',
//   sameSite: 'lax',
//   secure: true
// });

app.post('/logout-admin', (req, res) => {
  res.clearCookie('adminToken', {
    path: '/admin',
    domain: 'example.com',
    sameSite: 'lax',
    secure: true
  });
  res.send('Admin cookie cleared');
});

app.listen(3000);

How It Works

  1. Cookie was scoped. Login set path: '/admin' and a domain — that identity matters.
  2. Clear uses the same identity. Official rule: options must match what res.cookie() used.
  3. Ignore expires/maxAge. Passing them on clear has no effect; Express drops them.
  4. Browser then removes it. Only a matching clear header succeeds; a bare clear often fails silently.

Example 3 — Clear Multiple Cookies

Expire several cookies in one logout response, then end with 204.

Express
const express = require('express');
const app = express();

app.post('/signout', (req, res) => {
  res.clearCookie('token');
  res.clearCookie('refreshToken');
  res.clearCookie('theme');
  res.status(204).end();
});

app.listen(3000);

How It Works

  1. One call per name. Each clearCookie queues another Set-Cookie header.
  2. Same response. All three clear headers go out together with the status line.
  3. Match each cookie’s options. If refreshToken used a custom path, pass that path on its clear call.
  4. Empty body is fine. 204 + end() completes logout without a JSON payload.

Runtime Support

res.clearCookie() is an Express.js response method. It runs on a Node.js server (Express 5 needs Node.js 18+). Browsers apply the expired Set-Cookie; they do not execute this method.

✓ Server · Express 4 & 5

res.clearCookie()

Works wherever Express runs: Node.js is the primary target. Bun and Deno can run many Express apps with compatibility layers. Not available in the browser.

Server Node.js runtime API
Node.js 18+ for Express 5 · Express 4 supported
Supported
Bun Express-compatible server apps
Supported
Deno Via npm compatibility / adapters
Supported
res.clearCookie() Express on Node.js

Bottom line: Study and run these examples with Node.js and the express package. Copy a snippet into a local file and start the server with node.

Frequently Asked Questions

It sends a Set-Cookie header that expires the named cookie in the past, so compliant browsers remove it. The server does not delete cookies from the browser’s store by itself.
Browsers clear a cookie only when attributes such as path and domain match the ones used when the cookie was set. Mismatched options often look like a silent failure.
No. Express ignores expires and maxAge on clearCookie completely. Do not rely on them to clear a cookie.
No. clearCookie only writes response headers. cookie-parser is for reading cookies on the request (req.cookies).
Call res.clearCookie() once per cookie name in the same response, then send your body or status.

Next: res.cookie()

Set cookie names, values, and options on the response.

res.cookie() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful