ScreenshotNeo

BlogHow-to

Delete Matching Cookies with Puppeteer

Use Puppeteer’s BrowserContext.deleteMatchingCookies() to remove selected cookies from the intended context. Check your installed version’s filter type before relying on specific match fields.

By the ScreenshotNeo team4 October 20266 min read

Use await context.deleteMatchingCookies(...filters) to delete cookies matching the supplied filters from one Puppeteer BrowserContext. Cookies belong to a context, so select the context that owns the state you intend to change. The method returns a promise; await it before continuing.

The exact filter shape is version-dependent in the documentation available for this guide. Check the DeleteCookiesRequest type shipped with your installed Puppeteer version before supplying filters. Do not assume wildcard matching, partial-name matching, domain normalization, or path behavior.

1. Understand context scope and the matching API

Browser contexts isolate storage, including cookies and local storage. A launched browser has a default context and can have additional contexts. Calling a context method affects that context; it does not clear cookies from every context in the browser.

API Scope Use it when
BrowserContext.deleteMatchingCookies(...filters) The context on which it is called You know which context owns the cookies, including an explicitly created context.
Browser.deleteMatchingCookies(...filters) The browser’s default context The default context is deliberately the target.
Page.deleteCookie() Obsolete page-level API Do not use for new code; Puppeteer directs users to browser or context cookie methods.

The browser-level matching method is documented as a shortcut for calling browser.defaultBrowserContext().deleteMatchingCookies(). The two matching methods are choices of context scope, not documented alternatives with different filter behavior.

Official references: BrowserContext.deleteMatchingCookies(), BrowserContext, Browser shortcut, and obsolete Page.deleteCookie().

2. Check the filter type in your installed version

The method reference accepts one or more DeleteCookiesRequest values and returns Promise<void>, but the reference consulted here does not enumerate that request type’s fields or matching rules. Puppeteer’s cookie data documentation describes cookie properties including name, domain, path, and an optional partition key. That does not establish that every property is accepted as a deletion filter, nor how a given release compares it.

  1. Find the Puppeteer version resolved by your project and inspect that package’s declarations for DeleteCookiesRequest.
  2. Use only fields and semantics documented by that installed release.
  3. Keep filters narrow enough for the cookies you intend to delete, and inspect the target context’s cookies before and after if your workflow needs confirmation.

See the official CookieData interface for the documented cookie properties. Filter dimensions should not be inferred from cookie data fields alone.

3. Runnable JavaScript pattern

This complete example launches Chromium, creates an explicit context, navigates, inspects cookies, performs matching deletion, inspects again, and closes the browser. Replace the placeholder only with a filter object supported by the DeleteCookiesRequest declaration in your installed version. The placeholder is intentionally not presented as a valid request shape.

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch({ headless: true });
  try {
    const context = await browser.createBrowserContext();
    const page = await context.newPage();
    await page.goto('https://example.com', { waitUntil: 'domcontentloaded' });

    console.log('Before:', await context.cookies());

    // Replace this with a version-appropriate DeleteCookiesRequest.
    const filters = [/* inspect your installed Puppeteer type */];
    if (filters.length === 0) {
      throw new Error('Add a DeleteCookiesRequest supported by this Puppeteer version.');
    }
    await context.deleteMatchingCookies(...filters);

    console.log('After:', await context.cookies());
    await context.close();
  } finally {
    await browser.close();
  }
})().catch(error => {
  console.error(error);
  process.exitCode = 1;
});

Install Puppeteer in the project using its documented package instructions, then run the file with Node.js. The deletion call itself is await context.deleteMatchingCookies(...filters). There is no honest universal runnable filter literal to include without the installed version’s request schema.

If the default context is the intended target, use the documented shortcut:

await browser.deleteMatchingCookies(...filters);

For an explicit context, the equivalent ownership is visible in the call:

const context = page.browserContext();
await context.deleteMatchingCookies(...filters);

4. Inspect context cookies and verify scope

Use context.cookies() when inspecting cookies associated with the explicit context. The browser-level cookies() shortcut concerns the default context. Avoid inspecting one context and deleting from another by keeping the context variable alongside the page that uses it.

const context = page.browserContext();
const before = await context.cookies();

await context.deleteMatchingCookies(...filters);

const after = await context.cookies();
console.log({ before, after });

Cookie inspection helps establish what the context contains, but the deletion request’s filter semantics still come from the installed version’s type and documentation.

5. Troubleshooting

Symptom Likely cause What to do
TypeScript rejects the filter object The object does not match this version’s DeleteCookiesRequest. Inspect the declaration in the installed package and use its supported fields. Do not copy a filter shape from another release without checking.
No cookie appears to be deleted The filter may not match, or the call may target a different context from the page. Check page.browserContext(), inspect that context’s cookies, and verify the filter semantics for this version.
Cookies remain in another page or session That page may belong to another isolated context. Identify the owning context and call its matching deletion method. A context call does not clear every context.
Code fails after using Page.deleteCookie() The page-level cookie API is obsolete. Move to BrowserContext.deleteCookie() or BrowserContext.deleteMatchingCookies(), choosing the method that fits the task.
Default-context shortcut changes the wrong state browser.deleteMatchingCookies() is scoped to the default context. Call the method on the intended BrowserContext explicitly.
Assumed wildcard or partial matching does not behave as expected Those semantics are not established by the method reference used here. Do not assume them. Check the installed version’s API documentation and request type; provide explicitly supported filters.

6. Reliability, performance, and operational notes

  • Await completion. The operation is asynchronous and returns Promise<void>. Await it before navigating or taking a screenshot that depends on the changed cookie state.
  • Keep ownership explicit. Reuse the context associated with the page under test; this reduces mistakes caused by default-context assumptions.
  • Limit the affected state. Use an isolated context for independent jobs or tests when their cookie state should not be shared. Close a context when its work is finished.
  • Do not assume a speedup. The cited API references provide no deletion benchmark. For most automation flows, choose the operation for correct scope and measure your own end-to-end workload if latency matters.
  • Account for repeated setup. Creating a fresh context can isolate state, while deleting selected cookies can preserve other context state. Which is cheaper depends on the rest of the browser workflow; no comparative cost figure is documented here.
  • Version-pin behavior. Keep Puppeteer versions controlled in repeatable automation and review the installed request type when upgrading.

7. Or skip the browser setup

If your goal is a clean website screenshot rather than controlling a browser’s cookie jar, ScreenshotNeo is a website screenshot API and MCP server. Its capture flow accepts cookie and consent banners as a visitor and removes 60+ known consent platforms, newsletter popups, and chat widgets before the shot; each step can be turned off. Bot checks and CAPTCHAs, blank pages, timeouts, failed loads, and cache hits cost nothing, and response headers report the page verdict and billing status. AI agents can use its MCP tools, including take_screenshot, get_page_info, and capture_pdf.

One GET request returns an image or PDF. See the ScreenshotNeo API documentation for request options.

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
import requests

r = requests.get(
    "https://api.screenshotneo.com/v1/shot",
    params={"access_key": "YOUR_API_KEY", "url": "https://stripe.com"},
    timeout=90,
)
open("shot.webp", "wb").write(r.content)
const q = new URLSearchParams({ access_key: 'YOUR_API_KEY', url: 'https://stripe.com' });
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);

ScreenshotNeo includes full-page and element capture, viewport and device presets, PDF output, custom CSS and JavaScript, wait controls, request blocking, custom headers and cookies, caching, async jobs, bulk capture, and signed links. 1,000 screenshots a month are free with no card; paid plans start at $5 for 3,000 shots.

Sign up for 1,000 free screenshots a month, with no card required.

8. FAQ

Does matching deletion clear local storage?

No. This method is for cookies. Browser contexts isolate other storage too, but cookie deletion should not be treated as a general storage reset.

Can I delete cookies across every context with one context call?

No. The method is scoped to the context on which it is called. Identify each relevant context and act on it deliberately.

Does Puppeteer document a universal wildcard filter?

The method reference used here does not establish wildcard behavior. Check the request type and documentation for your installed release.