Clear all cookies
Expire every cookie that JavaScript can see on the current path.
function clearCookies() {
for (const cookie of document.cookie.split(';')) {
const name = cookie.split('=')[0].trim();
if (name) document.cookie = `${name}=; Max-Age=0; path=/`;
}
}
Usage
document.cookie = 'theme=dark; path=/';
document.cookie = 'lang=pt-BR; path=/';
clearCookies();
document.cookie; // → ''
Know the limits
- HttpOnly cookies, such as most session cookies, are invisible to JavaScript; only the server can clear them with Set-Cookie.
- A cookie set with another Path or Domain must be expired with the same attributes.
- The original set expires to the current time; Max-Age=0 expires immediately without relying on the client clock.
The function above is the code the usage lines run. It does not validate its arguments; add checks where it receives untrusted input.
Source & maintenance
Based on Clear all cookies from the original 1loc collection by Nguyen Huu Phuoc, with iamandrewluca. Copyright (c) 2020 phuoc-ng. MIT licensed.
Expires cookies with Max-Age=0; HttpOnly and path limits documented. Executed in Chromium. Original MDX retained as inert source evidence.
Maintained by Oveyon. Source commit: 14b481cf60df. Read the license.
Keep exploring
- Convert Celsius to Fahrenheit — Apply the temperature conversion without rounding the result.
- Convert hex to RGB — Expand #rgb if needed and read the red, green and blue channels.
- Convert URL parameters to object — Read a query string into an object; repeated keys become arrays.