How do you delete cookies in route handlers?
Unanswered
American Sable posted this in #help-forum
American SableOP
This works:
Neither of these work
cookies().set({
name: 'cookie',
value: 'monster',
httpOnly: true,
secure: true,
sameSite: 'strict',
path: '/bff/api',
expires: new Date(new Date().getTime() + 60 * 60 * 1000)
});Neither of these work
cookies().delete('cookie');
cookies().set({
name: 'cookie',
value: '',
maxAge: -1
});
cookies().set({
name: 'cookie',
value: '',
expires: -1
});27 Replies
Forest bachac
app dir or page dir?
American SableOP
app
American SableOP
Ok got it working. I had to include the path.
So if the existing cookie is
You have to delete it like this
Still not sure why delete doesn't work
So if the existing cookie is
cookies().set({
name: 'cookie',
value: 'monster',
httpOnly: true,
secure: true,
sameSite: 'strict',
path: '/api',
expires: new Date(new Date().getTime() + 60 * 60 * 1000)
});You have to delete it like this
cookies().set({
name: 'cookie',
value: '',
path: '/api',
expires: -1 // or 0
});Still not sure why delete doesn't work
@American Sable This works:
typescript
cookies().set({
name: 'cookie',
value: 'monster',
httpOnly: true,
secure: true,
sameSite: 'strict',
path: '/bff/api',
expires: new Date(new Date().getTime() + 60 * 60 * 1000)
});
Neither of these work
typescript
cookies().delete('cookie');
cookies().set({
name: 'cookie',
value: '',
maxAge: -1
});
cookies().set({
name: 'cookie',
value: '',
expires: -1
});
Forest bachac
Are you doing this in a route handler?
From the docs: You can only delete cookies in a Server Action or Route Handler.
@Forest bachac Are you doing this in a route handler?
American SableOP
Yes
Forest bachac
send more code
American SableOP
// src/app/test/route.ts
export async function GET() {
cookies().set({
name: 'cookie',
value: 'monster',
path: '/test',
maxAge: 60000
});
return NextResponse.json({ cookie: 'monster' });
}
export async function GET() {
cookies().delete('cookie');
return NextResponse.json({ cookie: 'monster' });
}Comment out the bottom function while setting the cookie. Once the cookie is set, switch out the top and with bottom and observe that the cookie remains in the browser.
Forest bachac
return NextResponse.json({ cookie: 'monster'
because your returning the cookie after you delete it..
because your returning the cookie after you delete it..
if your using cookies() you dont need to return a cookie
@American Sable
@Forest bachac return NextResponse.json({ cookie: 'monster'
because your returning the cookie after you delete it..
American SableOP
That's not a cookie. That's just json I'm returning to make sure the method is working. Sorry for the confusion. That json can be anything. It does not affect the cookie
return NextResponse.json({ foo: 'bar' });Forest bachac
have you tested it in prod? some browsers might mess with deleting cookies on http
@American Sable
if possible send the repo and the vercel url
American SableOP
I’m not on prod yet. I’ve tested it in chrome, Firefox, and edge. Honestly, I’m satisfied with my work around.
Forest bachac
alr
It works for me in development
Do you want me to send the repo
American SableOP
Sure. That’d be helpful. Thanks
Forest bachac
1 sec
Forest bachac
It works fine on my browser
also i believe the api routes like the stuff inside them could be cached when your doing what your doing, i would try with 2 different api routes or just doing npm run build, npm run start then creating cookie, then switching the code and doing that over again
American SableOP
Thanks. I’ll have a look when I’m back on my computer