Delete a Cookie with Puppeteer
Delete cookies with Puppeteer using the browser context that owns them. Compare exact-cookie deletion with filtered deletion, then verify the result.
Use browser.deleteCookie() to delete cookies from Puppeteer’s default browser context, or context.deleteCookie() for a separately created context. Cookies belong to their browser context, so deleting through the wrong one will not remove the cookie you are targeting. You can also use deleteMatchingCookies() when you want to remove cookies by filters.
Delete a cookie from the default context
After navigating to the site, pass a cookie-shaped object to the browser method. Puppeteer documents Browser.deleteCookie() as a shortcut for deleting cookies from the default browser context.
import puppeteer from 'puppeteer';
const browser = await puppeteer.launch();
try {
const page = await browser.newPage();
await page.goto('https://example.com');
const before = await browser.cookies();
console.log('Before:', before);
await browser.deleteCookie({
name: 'session',
value: 'example',
domain: 'example.com',
path: '/',
expires: -1,
httpOnly: false,
secure: false,
sourceScheme: 'NonSecure',
});
const after = await browser.cookies();
console.log('After:', after);
} finally {
await browser.close();
}
Install Puppeteer in your project with npm install puppeteer. This example uses ES modules; save it with an .mjs extension or set "type": "module" in package.json. The cookie fields shown follow the cookie object shape in Puppeteer’s documented examples. Supply values that match the cookie you intend to remove.
Delete a cookie from a specific browser context
If you created an isolated context, delete through that same context. Browser contexts have separate storage, including cookies and local storage. The browser-level convenience method applies only to the default context.
import puppeteer from 'puppeteer';
const browser = await puppeteer.launch();
try {
const context = await browser.createBrowserContext();
const page = await context.newPage();
await page.goto('https://example.com');
const before = await context.cookies();
console.log('Before:', before);
await context.deleteCookie({
name: 'session',
value: 'example',
domain: 'example.com',
path: '/',
expires: -1,
httpOnly: false,
secure: false,
sourceScheme: 'NonSecure',
});
const after = await context.cookies();
console.log('After:', after);
} finally {
await browser.close();
}
The verification reads cookies from the same context used for deletion. Puppeteer’s cookies() methods inspect the current cookie list; checking after deletion is a practical way to confirm whether the targeted cookie remains visible in that context.
Choose exact-cookie or filtered deletion
| Method | Scope | Use it when |
|---|---|---|
browser.deleteCookie(...cookies) |
Default browser context | You have cookie objects to delete from the default context. |
context.deleteCookie(...cookies) |
The selected browser context | The page or cookie belongs to an isolated context. |
browser.deleteMatchingCookies(...filters) |
Default browser context | You want the browser API’s filtered-deletion operation. |
context.deleteMatchingCookies(...filters) |
The selected browser context | You want filtered deletion within an isolated context. |
Use the current API reference for your installed Puppeteer version to determine the accepted filter fields and matching behavior. The filtered methods accept DeleteCookiesRequest filters; avoid assuming a filter matches more broadly or narrowly than the version’s documentation specifies.
Delete matching cookies
When the filter-based operation fits your task, use deleteMatchingCookies() on the context that owns the cookies. The following shows the call shape; consult the API reference for the filter fields supported by your installed release.
const filters = [
{
name: 'session',
domain: 'example.com',
},
];
await context.deleteMatchingCookies(...filters);
For the default context, use browser.deleteMatchingCookies(...filters). Both forms return a promise; await it before checking the resulting cookie list or continuing with work that depends on deletion.
Common errors and fixes
| Symptom | Likely cause | Fix |
|---|---|---|
| The cookie still appears after deletion. | The deletion ran against a different browser context. | Call deleteCookie() or deleteMatchingCookies() on the context that owns the page and its cookies. Check with that context’s cookies() method. |
browser.deleteCookie() does not affect an isolated page. |
The browser shortcut targets only the default context. | Use context.deleteCookie() for a page created from browser.createBrowserContext(). |
| Older code reports a deprecation or obsolete API warning. | It uses page.deleteCookie(). |
Migrate to browser.deleteCookie(), context.deleteCookie(), or the appropriate matching-cookie method. |
| The requested cookie is not found in the verification list. | The cookie may not be present in the context being inspected, or the cookie object does not describe the target as expected. | Inspect cookies() in the owning context and use the actual cookie details when selecting it for deletion. |
| A filtered deletion behaves differently than expected. | The code assumes filter semantics not guaranteed by the installed release’s reference. | Check the installed version’s DeleteCookiesRequest documentation and adjust the filter accordingly. |
Reliability and version notes
- Keep operations in the owning context. Context isolation is the key to predictable cookie cleanup.
- Await the deletion. These methods are asynchronous and return promises; wait before inspecting cookies or starting a dependent navigation.
- Verify in the same context. Read the cookie list through
browser.cookies()for the default context orcontext.cookies()for an isolated one. - Check your installed Puppeteer version. The referenced documentation pages reflect different version snapshots, so confirm method signatures and deprecation status against your project’s installed release.
Deleting a browser cookie is separate from deleting application data held elsewhere, such as server-side session state or site data stored outside the browser cookie jar. If the application restores a session after the cookie is removed, inspect the site’s own authentication flow and the storage mechanisms it uses.
Or skip the browser setup
If your goal is to inspect a page rather than manage browser state, ScreenshotNeo returns a screenshot or PDF from one API request. Its capture can accept cookie and consent banners and remove 60+ known consent platforms, newsletter popups, and chat widgets before taking the shot. Bot checks, blank pages, failed loads, timeouts, and cache hits are not billed, and responses identify the page verdict and billing status. Its MCP server gives AI agents tools to take screenshots, inspect page information, and capture PDFs. The free plan includes 1,000 screenshots a month with no card; paid plans start at $5 for 3,000.
See the ScreenshotNeo API documentation. For example, this cURL request saves a WebP screenshot:
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://example.com -o shot.webp
Equivalent Python:
import requests
r = requests.get(
"https://api.screenshotneo.com/v1/shot",
params={"access_key": "YOUR_API_KEY", "url": "https://example.com"},
timeout=90,
)
open("shot.webp", "wb").write(r.content)
Equivalent Node.js:
const q = new URLSearchParams({ access_key: 'YOUR_API_KEY', url: 'https://example.com' });
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);
if (!res.ok) throw new Error(`Screenshot request failed: ${res.status}`);
await import('node:fs/promises').then(({ writeFile }) => writeFile('shot.webp', Buffer.from(await res.arrayBuffer())));
Create a free ScreenshotNeo account for 1,000 screenshots a month, with no card required.
FAQ
Does deleting a cookie clear local storage too?
No. Cookies and local storage are distinct browser storage types. Cookie deletion removes cookies through the browser context API; handle other storage separately if your task requires it.
Does deleteCookie() return the deleted cookie?
No. The documented return type is Promise<void>. Inspect the context’s cookie list separately if you need to verify the result.
Should I keep using page.deleteCookie()?
No. Puppeteer marks the page-level cookie API obsolete and deprecated; use the browser or browser-context methods instead.


