> ## Documentation Index
> Fetch the complete documentation index at: https://chainstack-docs-polygon-erigon-trace-deprecation.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# clearSubscriptions | Gnosis

> Unsubscribe from all active Gnosis Chain ethers.js subscriptions at once using removeAllListeners. Useful for cleanup when resetting your event listeners.

The subscriptions available using the ethers.js `provider.on` method are:

<CardGroup cols={2}>
  <Card title="on('block')" icon="angle-right" iconType="solid" href="/reference/gnosis-subscribenewblockheaders" horizontal />

  <Card title="on(filter)" icon="angle-right" iconType="solid" href="/reference/gnosis-subscribelogs" horizontal />

  <Card title="eth_syncing" icon="angle-right" iconType="solid" href="/reference/gnosis-subscribesyncing" horizontal />
</CardGroup>

## Parameters

`boolean` — keep the [eth\_syncing](/reference/gnosis-subscribesyncing) subscription if `true`.

## Response

`boolean` — the boolean value indicating if the subscriptions were removed successfully. `true` if removed successfully, `false` if not.

## `clearSubscriptions` code example

<CodeGroup>
  ```javascript index.js theme={null}
  const ethers = require("ethers");
  const NODE_URL = "CHAINSTACK_WSS_URL";
  const provider = new ethers.WebSocketProvider(NODE_URL);

  async function clearSubscriptions() {
      try {
        await provider.removeAllListeners();
        console.log("Subscriptions were canceled successfully");
        return process.exit(1)

      } catch (error) {
        console.error("Failed to cancel subscriptions:", error);
        return process.exit(1)
      }
    }

  clearSubscriptions()
  ```
</CodeGroup>

## Use case

A way to use the `clearSubscriptions` method is to place it in a timer to clear all of the subscriptions after a pre-determined about.

The following code is an example using the [block event](/reference/gnosis-subscribenewblockheaders) subscription, which will be stopped and removed after one minute using `clearSubscriptions`:

<CodeGroup>
  ```javascript index.js theme={null}
  const ethers = require("ethers");
  const NODE_URL = "CHAINSTACK_WSS_URL";
  const provider = new ethers.WebSocketProvider(NODE_URL);

  async function subscribeToNewBlocks() {
      try {
          // Attach event listeners for new blocks and errors
          provider.on('block', handleNewBlock);
          provider.on('error', handleError);
          console.log("Subscribed to new blocks");
      } catch (error) {
          console.error(`Error subscribing to new blocks: ${error}`);
      }
  }

  /* Fallback functions to react to the different events */

  // Event listener that logs the received block number
  function handleNewBlock(blockNumber) {
      console.log(blockNumber);
  }

  // Event listener that logs any errors that occur
  function handleError(error) {
      console.error(`Error receiving new blocks: ${error}`);
  }

  async function clearSubscriptions() {
      try {
          await provider.removeAllListeners();
          console.log("Subscriptions were canceled successfully");
          return process.exit(1)

      } catch (error) {
          console.error("Failed to cancel subscriptions:", error);
          return process.exit(1)
      }
  }

  async function main() {

      subscribeToNewBlocks();

      // Run clearSubscriptions() once after 60 seconds
      setTimeout(clearSubscriptions, 60000);
  }

  main()
  ```
</CodeGroup>

The code starts by defining an async function called `subscribeToNewBlocks` that subscribes to the `block` event using the `provider.on` method. Every time a new block is mined, ethers.js emits the `block` event with the new block number. The code defines two event listener functions, `handleNewBlock` and `handleError`, that log messages to the console when these events occur.

The `subscribeToNewBlocks` function is called from the `main` function, which is also defined in the code. The `main` function calls `subscribeToNewBlocks` to create a new subscription and then schedules the `clearSubscriptions` function to run once after 60 seconds using the `setTimeout` function. The `clearSubscriptions` function uses the `provider.removeAllListeners` method to cancel any existing subscriptions and log a message to the console if the cancellation was successful.
