Skip to main content

[JavaScript] How do I dynamically disable and enable Sentry after it has been initialized?

Issue

I need to turn Sentry event sending on or off at runtime after Sentry.init, based on application criteria such as consent or user settings.

Applies To

  • All SaaS Customers and Self-Hosted Users

  • JavaScript SDK

  • SDK Setup

Resolution

Do not shut down and re-initialize the SDK to toggle Sentry. Calling Sentry.close() and then Sentry.init() again can create multiple clients and unexpected behavior.

To stop or resume sending events after initialization, set enabled on the current client:

const client = Sentry.getClient();
if (client) {
  client.getOptions().enabled = false; // stop sending events
  // client.getOptions().enabled = true; // resume sending events
}

You can also set enabled in Sentry.init when you know the decision at startup.

Setting enabled to false stops the SDK from sending events to Sentry. It does not remove instrumentation or eliminate runtime overhead.

If you need to disable Sentry for a whole environment at startup, see How do I disable Sentry in specific environments?.

Did this answer your question?