Next.js Discord

Discord Forum

how can I delete a cookie on the client side in Next.JS app router

Unanswered
Seppala Siberian Sleddog posted this in #help-forum
Open in Discord
Avatar
Seppala Siberian SleddogOP
I want to delete, manipulate cookies on the client side how can I achieve that in Next.JS app router

3 Replies

Avatar
Chinese Crested Dog
You cannot directly delete a cookie, but you can overwrite it and set its expiration date to a past date, effectively removing it.

const deleteCookie = (name: string) => {
  document.cookie = name + '=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}

deleteCookie("test");


NOTE: Make sure the code is ran on the client.
Avatar
Chinese Crested Dog
nextjs cookies can only be used on the server side, the question that was asked was 'how can I delete a cookie on the client side in Next.JS app router'
Avatar
Chinese Crested Dog
thank you for deleting that