Back to Knowledge Hub
Getting Started

Playwright Clear Cookies Versus a New Context: What Each Reset Removes

clearCookies() wipes the cookie jar alone, a fresh context empties all three state surfaces, and a fresh browser redraws the profile boundary. Measured on one owned page.

BotBrowser Team

Documentation

Want the structured docs for Getting Started?

This article lives in the editorial library. For step-by-step setup, reference material, and ongoing updates, jump into the docs section.

Three resets over one owned page: clearCookies() leaves the cookie gone but localStorage and the service worker alive, a newContext() in the same browser empties all three surfaces, and a launch() of a second browser instance records the same all-empty state.

A test that only needs a logged-out page does not need the same reset as a test that needs an empty origin. "Playwright clear cookies" names one narrow operation, context.clearCookies(), yet the phrase gets read as three different promises: wipe the cookie jar, wipe everything the page ever stored, or hand the test a browser that has never met the site. Those are three different resets with three different scopes, and picking the wrong one leaves state behind that the next assertion then trips over. BotBrowser profiles keep their own persistent storage, so pointing a test at a fresh profile is a stronger reset than clearing cookies inside a reused profile-backed context; BotBrowser adds no cookie-clearing API beyond what Chromium and Playwright already expose, so the choice between clearCookies(), a new context and a new browser is the same choice on a BotBrowser launch as on a stock one.

The owned run below serves one page from 127.0.0.1, lets it write a session cookie, a localStorage key and one service worker registration, then applies each reset in turn and reads back what survived. The fixture is docs/evidence/playwright-clear-cookies-versus-new-context-fixture-20261125.mjs, the run record is docs/evidence/playwright-clear-cookies-versus-new-context-run-20261125.json, and the browser is an installed stock Chrome build, version 158.0.8127.89, driven headless through Playwright. No third-party request is made; all traffic stays on 127.0.0.1.

TL;DR

  • context.clearCookies() removes cookies and nothing else. The owned run seeded a cookie, a localStorage key and a service worker on one context; after clearCookies() the cookie read false while localStorage and the worker both survived, worker count still 1.
  • A fresh context in the same browser emptied all three surfaces. A newContext() load of the same origin read cookie false, storage false, worker false, worker count 0: every Playwright context is its own storage partition.
  • A fresh browser instance recorded the same all-empty state. chromium.launch() a second time, open a context, load the origin, and all three reads come back empty; the reset boundary is the profile.
  • clearCookies() does not touch localStorage, cache or service workers. Reading a cookie clear as a full state reset is the misdiagnosis the comparison below separates.
  • Match the reset to the state that must disappear: cookie jar only, all three page-visible surfaces, or the whole profile.
  • BotBrowser profiles keep their own persistent storage, so a fresh profile is a stronger reset than clearing cookies inside a reused profile-backed context; BotBrowser adds no cookie-clearing API beyond what Playwright already documents.

Table of contents

The browser cookie partitioning and CHIPS article explains how browsers split one origin's state across partitions, and the cookie management page treats cookies as a product surface to load, rotate and retire. The download event and save path page covers another place where per-context state decides what a test observes. What follows keeps the lens narrower: one page, three state surfaces, three resets.

Three reset scopes over one owned page

The Playwright clearCookies reference documents the call as removing all cookies from the context. The BrowserContext reference describes the context as an isolated session: separate cookies, separate storage, separate service worker registrations. The Set-Cookie documentation describes how a page plants cookie state in the first place. Put together, the three surfaces a page can leave behind look like this:

SurfaceWhat the owned page writesRemoved by clearCookies()Removed by a new contextRemoved by a new browser
Cookie jarsession=login-look-alike; path=/YesYesYes
localStoragekey cart set to keptNoYesYes
Service workerregistration for /sw.jsNoYesYes

The cookie jar is context state by definition: document.cookie writes into the jar the context owns, and clearCookies() empties that jar. localStorage is origin storage inside the context's storage partition, which is a different bucket with a different lifetime. A service worker registration is a third bucket again: the registration binds an origin to a script inside the partition, and nothing about it lives in the cookie jar. Where that partition sits in the context lifecycle, and what the context carries versus what the browser carries, is the Playwright browser context lifecycle and storage comparison.

That separation is why the word "reset" needs a scope. A reset that empties the cookie jar leaves the other two buckets intact, and a test that reads them back sees a page that is half first-visit and half returning visitor. The owned run makes each scope observable on the same page, so the distinction is a measurement rather than a definition.

The fixture starts a plain Node HTTP server on 127.0.0.1 and serves one page, /state.html. A seeded visit to /state.html?seed=1&reg=1 runs three writes in the page: document.cookie set to session=login-look-alike; path=/, localStorage.setItem('cart', 'kept'), and navigator.serviceWorker.register('/sw.js'). A readState() helper in the page then reports four values: whether the cookie is present, whether the localStorage key survived, whether any service worker registration exists, and the registration count. The browser is an installed stock Chrome build, version 158.0.8127.89, launched headless through Playwright with executablePath pointed at that binary.

Three cases run against that one page:

  1. clear-cookies-on-used-context (primary): seed the context, call context.clearCookies() on the used context, read the state back through the same page.
  2. fresh-context-same-browser (counter): open a new context in the same browser, load the same origin with no seed, read the state.
  3. fresh-browser-instance (boundary): launch a second browser, open a context, load the same origin with no seed, read the state.

The fixture writes one summary line covering the three case outcomes to stdout; that line reads:

{"status":"passed","cases":[{"case":"clear-cookies-on-used-context","status":"passed","seeded":{"cookie":true,"storage":true,"worker":true,"workerCount":1},"afterClearCookies":{"cookie":false,"storage":true,"worker":true,"workerCount":1}},{"case":"fresh-context-same-browser","status":"passed","state":{"cookie":false,"storage":false,"worker":false,"workerCount":0}},{"case":"fresh-browser-instance","status":"passed","state":{"cookie":false,"storage":false,"worker":false,"workerCount":0}}]}

The measured record from docs/evidence/playwright-clear-cookies-versus-new-context-run-20261125.json is:

CaseReset appliedSurviving state
clear-cookies-on-used-contextclearCookies() on the seeded contextcookie false; storage true; worker true; workerCount 1
fresh-context-same-browsernewContext() in the same browser, then load the origincookie false; storage false; worker false; workerCount 0
fresh-browser-instancesecond launch(), new context, then load the origincookie false; storage false; worker false; workerCount 0

The three resets in code:

// Case 1: clear the cookie jar, keep the context.
await context.clearCookies();

// Case 2: a fresh context in the same browser is a clean partition.
const fresh = await browser.newContext();
const freshPage = await fresh.newPage();
await freshPage.goto(url);

// Case 3: a fresh browser instance is a clean profile.
const second = await chromium.launch({ headless: true });
const secondContext = await second.newContext();
const secondPage = await secondContext.newPage();
await secondPage.goto(url);

Read the middle line against the first: the fresh context did not inherit the cookie, the localStorage key or the worker registration, even though the browser process already had a context that held all three. Read the third line against the second: launching a whole second browser produced the same three zeros on the owned page. The interesting distance in the data is between case 1 and case 2, because that distance is where the "clear cookies" misdiagnosis lives.

What readState() reads and what it does not

The four values come from inside the page: document.cookie.includes('session=login-look-alike'), localStorage.getItem('cart') === 'kept', and navigator.serviceWorker.getRegistrations(). That is the state a page-level assertion sees, which is the level where the wrong reset shows up. The record does not claim anything about the HTTP cache, IndexedDB, or third-party cookie behaviour: the run measured three surfaces on one owned page served from 127.0.0.1, and the claims below stay inside that boundary. How those other stores differ from cookies is the browser storage model comparison.

The primary case is the one most tests actually run. The context was used: it held a session cookie, a localStorage key and a live service worker registration, all written by the same page load. Then context.clearCookies() ran once, and the same page read its own state again. The cookie was gone. The localStorage key still read kept. The service worker registration was still there, count 1.

That asymmetry is not a bug or a partial failure; it is the documented scope of the call. The Playwright clearCookies reference promises cookie removal from the context, and the owned run confirms exactly that scope and nothing wider. localStorage lives in the origin's storage partition, which the call never opens. The service worker registration lives in its own registry, which the call also never opens.

The practical failure shows up one assertion later. A login-look-alike flow that stores a cart, a draft or a feature flag in localStorage keeps all of it after a cookie clear, because that state was never cookie state. A test that wants "the site has never seen this visitor" and reaches for clearCookies() gets a visitor whose jar is empty but whose storage and workers are intact, and the next expect reads the leftover state. The fix is not a second call to something like clearStorage(); Playwright has no such method on a used context, and the reset that actually empties the partition is a new context.

The same logic runs in the other direction for the worker. Because clearCookies() leaves the registration alive, the worker keeps controlling pages after a cookie clear, and any state it holds or serves comes from before the reset. The owned run measured the registration count, not what the worker served; the claim stops at "the registration survived", which is what the count shows.

A fresh context empties all three surfaces

The counter case answers the question the primary case raises: if a cookie clear is too narrow, what is the reset that clears everything the page can see? The answer in the run is browser.newContext(). A new context in the same browser, loading the same origin with no seed, read cookie false, storage false, worker false, worker count 0.

Nothing was deleted from the old context to make that happen. The seeded context continued to exist with its own partition, and the fresh context simply never shared it. Each Playwright browser context is an isolated storage partition: its own cookie jar, its own origin storage, its own service worker registrations. A page in the fresh context is a first-time visitor by construction, not by cleanup. That partition-per-context construction is the boundary multiple browser profiles: what stays separate maps across full profiles: cookies and site storage follow the context, while process-level state does not.

That construction is what makes the fresh context the reset that matches "the site has never seen this visitor". The page's own reads agree on all three surfaces at once, in one load, with no sequence of clear calls to get wrong. The cookie management page shows the complementary move for tests that need a non-empty known state: load a prepared cookie set before the first navigation instead of cleaning up after a previous run.

Two boundaries keep the claim honest. First, the fresh context and the seeded context shared one browser process; the run did not measure profile-level state outside the three page-readable surfaces, so nothing here extends to the HTTP cache or other process-wide state. Second, "clean" means clean relative to the origin the fixture served: the fresh context was never navigated anywhere else, and a real suite that navigates to other origins gets the same isolation per context, not a guarantee about any particular origin it has polluted.

A fresh browser instance redraws the profile boundary

The boundary case launches a second browser with chromium.launch(), opens a context inside it, and loads the same origin. All three reads came back empty, exactly as in the counter case. On the page-visible surfaces this run measured, a new browser instance and a new context produced the same result.

The difference sits one level down, in what each reset redraws. A new context redraws the storage partition inside a browser that already runs. A new browser starts a new process with a fresh profile area, so everything the profile would have carried, whatever its scope, starts empty. Playwright's default chromium.launch() uses a non-persistent profile for the browser it starts, so the second launch had nothing to inherit from the first.

The run demonstrates the new browser once, as a boundary: the strongest reset in the comparison, and the same three zeros. It does not claim a new browser is required for every isolation need, because the counter case already emptied all three page-visible surfaces at lower cost. A second launch() pays for a second browser process, which matters in CI where the suite may launch dozens of workers; the fresh context is the cheaper reset whenever a partition boundary is enough. Where a genuine profile boundary is the requirement, for example per-worker identity with persistent storage, a fresh profile is the stronger reset, and that is the capability profile-backed browsers sell.

BotBrowser capability and limitation

BotBrowser's Playwright setup page documents profile-backed contexts: a launch that loads a profile with its own persistent storage, so the browser surfaces as a repeatable identity instead of an empty one. That capability changes the meaning of the resets in this comparison. Clearing cookies inside a reused profile-backed context empties the jar but leaves the profile's persistent storage in place, so a fresh profile is a stronger reset than a cookie clear in that world. The launcher guide documents how the real binary starts with that profile. BotBrowser documents that setup path, and it does not widen clearCookies() past the cookie jar.

The limitation sits in the same paragraph: BotBrowser adds no cookie-clearing API beyond what Chromium and Playwright already expose. clearCookies() on a BotBrowser context does what it does on a stock context, no more and no less, and the choice between a cookie clear, a new context and a fresh profile is decided by the same storage scopes measured above. The BotBrowser documentation covers profile-backed contexts; it does not compare clearCookies() with a fresh context or a fresh browser across cookies, storage and workers, which is the comparison the owned run supplies.

Practical conclusion

Read the three resets by what each one deletes. context.clearCookies() empties the cookie jar: the run's primary case read cookie false with storage and the worker both alive at count 1. A fresh context in the same browser empties all three page-visible surfaces at once: the counter case read cookie, storage and worker all false with count 0, because each context is its own storage partition. A fresh browser instance starts a new process with a fresh profile and read the same three zeros, which makes it the boundary of the comparison rather than a daily necessity. Match the reset to the state that must disappear, reach for newContext() when the test needs a first-visit origin, and reserve a second launch() for the cases where the profile itself is the unit of isolation. BotBrowser profiles keep their own persistent storage, so on a profile-backed launch the fresh profile is the stronger reset, and no extra cookie-clearing API exists on top of the ones Playwright already documents.

Sources

Keep reading

FAQ

Does context.clearCookies() clear localStorage or service workers?

No. The owned run seeded all three surfaces on one context, called clearCookies() once, and read the cookie gone while the localStorage key and the service worker registration survived, count 1. The call's documented scope is cookies, and the measured record agrees with that scope.

Do I need a new browser instance for full isolation?

For the three page-visible surfaces the run measured, no: a fresh context in the same browser already read cookie, storage and worker all empty. The fresh browser instance produced the same three zeros and redraws the profile boundary instead of the partition boundary. The record shows it once as a boundary case and does not claim it is required for every isolation need.

Does a fresh context keep anything from the old context?

Nothing on the measured surfaces. The fresh context loaded the same origin as a first-time visitor: no cookie, empty localStorage, no service worker registrations. The two contexts did share a browser process, and the run made no measurement of process-level state beyond the three surfaces, so the claim stops there.

How does this map to BotBrowser profiles?

A BotBrowser profile keeps its own persistent storage, so a fresh profile is a stronger reset than clearing cookies inside a reused profile-backed context. BotBrowser adds no cookie-clearing API beyond what Chromium and Playwright expose, so clearCookies(), newContext() and a fresh profile mean the same operations on a BotBrowser launch as on a stock one.

Why did my test still see old state after clearing cookies?

Because the state it read was never cookie state. A localStorage key, a service worker registration, or any assertion that reads them survives a cookie clear, as the primary case shows. Use a fresh context when the assertion needs an empty origin, and keep the cookie clear for the cases where the jar is genuinely the only surface that matters.

#Playwright Clear Cookies#clearCookies#newContext#localStorage#Service Worker

This article is part of the guide: Browser Automation Getting Started: A First Run Map

Run the same check in BotBrowser

Load a profile, keep the signals you just read about consistent inside the browser, and confirm the result on your own machine before a rollout.