Clearing Cookie in Next.js

Photo by Mollie Sivaram on Unsplash

Clearing Cookie in Next.js

Vamsi Rao's photo
Vamsi Rao
ยทFeb 3, 2022ยท

1 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

Like any other header, a cookie is also a header that web apps use to store essential information. If you want to clear a cookie from server-side in Next.js (which uses node.js under the hood) you can simply use response's setHeader method like so:

res.setHeader('Set-Cookie', 'some-cookie=someValue; Max-Age=0');

The important part is setting Max-Age=0, which will make the browser delete that cookie immediately.


PS: This will work for other frameworks and languages too, syntax might be different but Headers work in the same way across all browsers

ย 
Share this