How to delete cookie in Express and JavaScript?

Sometimes, we want to delete cookie in Express and JavaScript.

In this article, we’ll look at how to delete cookie in Express and JavaScript.

How to delete cookie in Express and JavaScript?

To delete cookie in Express and JavaScript, we can use the res.clearCookie method.

For instance, we write:

const express = require('express')
const app = express()
const port = 3000

app.get('/logout', (req, res) => {
  res.clearCookie('cookie');
  return res.sendStatus(200);
})


app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})

to call res.clearCookie with the key of the cookie to delete in the /logout endpoint.

Conclusion

To delete cookie in Express and JavaScript, we can use the res.clearCookie method.