Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Detach a Puppeteer CDP session with await client.detach(). This disconnects that CDPSession from its target; Puppeteer’s documentation says the detached session no longer emits events and cannot send messages. The method documentation does not say that it closes the page or browser.

Detach the session

Assuming page is an existing Puppeteer Page, create a session and await its detach() method when you are finished using it:

const client = await page.createCDPSession();

// Use client.send(...) or listen for CDP events while needed.
await client.detach();

Page.createCDPSession() returns a Promise<CDPSession>. In Puppeteer 25.12.0, CDPSession.detach() returns Promise<void>. See the Page.createCDPSession() reference and CDPSession.detach() reference.

Use try/finally when cleanup must run after errors

If your work can throw and you want to make the cleanup order explicit, put the operation in a try block and detach in finally:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const client = await page.createCDPSession();

try {
  await client.send('Runtime.enable');
  // Perform the CDP work that needs this session.
} finally {
  await client.detach();
}

This pattern ensures your code reaches the awaited cleanup when the work succeeds or throws. The API reference does not state that detaching is idempotent, so do not rely on repeated calls being harmless.

What changes after detaching

A CDPSession carries raw Chrome DevTools Protocol traffic: send() sends protocol methods, and event listeners receive protocol events. Once detached, Puppeteer documents that the session object “won’t emit any events and can’t be used to send messages.” See the CDPSession.send() reference.

  • Do not continue using the detached object to send CDP commands.
  • Do not expect it to deliver further events.
  • If you need CDP access again, create a session with page.createCDPSession() and use that new session.

The API documents a readonly detached property and a sessiondetached event as observation surfaces. The cited references do not specify when the property changes or how the event is ordered relative to the detach promise resolving. See the CDPSession class reference and CDPSessionEvents reference.

Detaching is not closing the page or browser

detach() describes detaching the CDP session from its target. Its API documentation does not say that this call closes the Puppeteer Page or the browser. Treat session cleanup and page or browser shutdown as separate lifecycle actions; use the appropriate page or browser lifecycle method when you intend to close those objects.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Check your installed Puppeteer version

The method and page-creation references cited here identify Puppeteer 25.12.0. Puppeteer’s API can change between releases, so if your installed package is another version, consult the API documentation matching that release rather than assuming the same details or timing behavior.

Troubleshooting

The session cannot send a command

If the session has already been detached, it cannot be used to send messages. Create a new session from the page with await page.createCDPSession() if CDP access is needed again.

You expected detaching to close the page

Detachment applies to the CDP session, not a documented page- or browser-close operation. Close the page or browser separately using the lifecycle operation appropriate to your application.

You are unsure whether detachment has completed

Await the promise returned by detach() to make the cleanup order explicit. The cited API references do not provide more detailed timing guarantees for the detached property or sessiondetached event.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Or skip the browser setup

If your goal is a website screenshot rather than raw CDP work, ScreenshotNeo provides a screenshot API and MCP server. One GET request can return an image or PDF; for example, this cURL call saves a WebP screenshot:

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

See the ScreenshotNeo API documentation for request options. It removes cookie banners, popups and chat widgets before capture; bot checks, blank pages and failed loads are never billed. An MCP server lets AI agents take screenshots. The free plan includes 1,000 screenshots a month with no card, and paid plans start at $5 for 3,000. Sign up for ScreenshotNeo.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.