# Clear all cookies

Expire every cookie that JavaScript can see on the current path.

Canonical: https://1loc.dev/misc/clear-all-cookies/
Version: 1.0.0
Runtime: JavaScript · modern browsers (DOM)
Verification: Usage lines executed in headless Chromium against the fixture markup; no external services
Updated: 2026-10-28

## Implementation

```javascript
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

```javascript
document.cookie = 'theme=dark; path=/';
document.cookie = 'lang=pt-BR; path=/';
clearCookies();
document.cookie; // → ''
```

Lines marked → were executed in headless Chromium against the markup above, and each result was compared with the value shown. Lines marked e.g. are illustrative.

## 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 does not validate its arguments. Add checks where it receives untrusted input.

## Source and license

Original: https://github.com/phuocng/1loc/blob/14b481cf60dff1620ab1bb99a43b1f68068cdd9b/contents/clear-all-cookies.mdx
Author: Nguyen Huu Phuoc and original contributors
Contributors: iamandrewluca
Source commit: 14b481cf60dff1620ab1bb99a43b1f68068cdd9b
Copyright (c) 2020 phuoc-ng
License: MIT — https://1loc.dev/licenses/
Maintainer: Oveyon
Modifications: Expires cookies with Max-Age=0; HttpOnly and path limits documented. Executed in Chromium. Original MDX retained as inert source evidence.
