> ## Documentation Index
> Fetch the complete documentation index at: https://docs.chainstack.com/llms.txt
> Use this file to discover all available pages before exploring further.

<AgentInstructions>

## Submitting Feedback

If you encounter incorrect, outdated, or confusing documentation on this page, submit feedback:

POST https://docs.chainstack.com/feedback

```json
{
  "path": "/reference/gnosis-clearsubscriptions",
  "feedback": "Description of the issue"
}
```

Only submit feedback when you have something specific and actionable to report.

</AgentInstructions>

# clearSubscriptions | Gnosis

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

The subscriptions available using the `web3.eth.subscribe` methods are:

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

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

  <Card title="subscribe('syncing')" icon="angle-right" iconType="solid" href="/reference/gnosis-subscribesyncing" horizontal />
</CardGroup>

<Check>
  ### Get your own node endpoint today

  [Start for free](https://console.chainstack.com/) and get your app to production levels immediately. No credit card required.

  You can sign up with your GitHub, X, Google, or Microsoft account.
</Check>

## Parameters

`boolean` — keep the [subscribe("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={"system"}
  const Web3 = require("web3");
  const NODE_URL = "CHAINSTACK_WSS_URL";
  const web3 = new Web3(NODE_URL);

  async function clearSubscriptions() {
      try {
        await web3.eth.clearSubscriptions();
        console.log("Subscriptions were cancelled 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 [subscribe("newBlockHeaders")](/reference/gnosis-subscribenewblockheaders) method, which will be stopped and removed after one minute using `clearSubscriptions`:

<CodeGroup>
  ```javascript index.js theme={"system"}
  const Web3 = require("web3");
  const NODE_URL = "CHAINSTACK_WSS_URL";
  const web3 = new Web3(NODE_URL);

  async function subscribeToNewBlocks() {
      try {
          // Create a new subscription to the 'newBlockHeaders' event
          const subscription = await web3.eth.subscribe('newBlockHeaders');

          // Attach event listeners to the subscription object
          subscription.on('connected', handleConnected);
          subscription.on('data', handleNewBlock);
          subscription.on('error', handleError);
      } catch (error) {
          console.error(`Error subscribing to new blocks: ${error}`);
      }
  }

  /* Fallback functions to react to the different events */

  // Event listener that logs a message when the subscription is connected
  function handleConnected(subscriptionId) {
      console.log(`New subscription: ${subscriptionId}`);
  }

  // Event listener that logs the received block header data
  function handleNewBlock(blockHeader) {
      console.log(blockHeader);
  }

  // Event listener that logs any errors that occur
  function handleError(error) {
      console.error(`Error receiving new blocks: ${error}`);
  }

  async function clearSubscriptions() {
      try {
          await web3.eth.clearSubscriptions();
          console.log("Subscriptions were cancelled 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 creates a new subscription to the `newBlockHeaders` event using the `web3.eth.subscribe` method. This method returns a subscription object that can be used to attach event listeners for different events, such as `connected`, `data`, and `error`. The code defines three event listener functions `handleConnected`, `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 `web3.eth.clearSubscriptions` method to cancel any existing subscriptions and log a message to the console if the cancellation was successful.
