Skip to content
the poor coder

delete all cookies express

Poor Man
Published
Reading time
2 min read
On this page

How to delete all cookies in Express#

If you're working with the Express framework in Node.js, you may need to delete all cookies at some point. This could be for security reasons, to clear session data, or for any other reason. Here's how you can do it:

If you're using the cookie-parser middleware in your Express application, you can use its clearCookie method to delete all cookies.

Text
app.get('/logout', (req, res) => {
  res.clearCookie('cookie1');
  res.clearCookie('cookie2');
  // ... clear all cookies this way
  res.redirect('/');
});

This code will clear the cookies named cookie1 and cookie2, but you can replace those with your own cookie names. If you have a lot of cookies to clear, it may be more efficient to loop through them instead of calling clearCookie multiple times.

If you're not using cookie-parser, or if you want to delete cookies from a different domain or path than the one you're currently on, you can use the Set-Cookie header to delete cookies.

Text
app.get('/logout', (req, res) => {
  res.set('Set-Cookie', 'cookie1=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT');
  res.set('Set-Cookie', 'cookie2=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT');
  // ... clear all cookies this way
  res.redirect('/');
});

This code will delete the cookies named cookie1 and cookie2, but you can replace those with your own cookie names. The Path attribute specifies the path for which the cookie is valid (in this case, the root path), and the Expires attribute specifies a date in the past to indicate that the cookie has expired.

Option 3: Using a library#

If you don't want to write your own code to delete cookies, you can use a library like cookie or cookies. These libraries provide a simple API for working with cookies, including deleting them.

Text
// Using the 'cookie' library
const cookie = require('cookie');

app.get('/logout', (req, res) => {
  const cookies = cookie.parse(req.headers.cookie || '');
  for (const name in cookies) {
    res.set('Set-Cookie', cookie.serialize(name, '', { path: '/', expires: new Date(0) }));
  }
  res.redirect('/');
});

// Using the 'cookies' library
const Cookies = require('cookies');

app.get('/logout', (req, res) => {
  const cookies = new Cookies(req, res);
  cookies.keys().forEach(name => {
    cookies.set(name);
  });
  res.redirect('/');
});

These examples use the serialize method of the cookie library and the set method of the cookies library to delete all cookies. The serialize method creates a Set-Cookie header with an empty value and an expiration date in the past, and the set method of the cookies library does the same thing.

Whichever option you choose, make sure you understand the implications of deleting cookies in your application. Deleting cookies may cause users to lose their session data or require them to log in again.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC