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

# connection-removed

> Fires when a connection is removed from an app

This event is triggered whenever a user removes a
[connection](../connections/connections). The registered handler runs before
the connection is deleted, meaning that if the handler throws an error, the
connection will not be removed. You can use the handler to perform any necessary
teardown work.

## Example

Here's an example that cleans up a [webhook handler](../webhooks/webhook-handlers).

```ts TypeScript theme={"system"}
// src/events/connection-removed.event.ts
import {listWebhookHandlers, deleteWebhookHandler} from "attio/server"
import type {Connection} from "attio/server"

export default async function connectionRemoved({connection}: {connection: Connection}) {
  const webhookHandlers = await listWebhookHandlers()

  for (const webhookHandler of webhookHandlers) {
    const acmeResponse = await fetch(
      `https://api.acmeinc.com/api/v1/webhooks${webhookHandler.externalWebhookId}`,
      {
        method: "DELETE",
        headers: {
          Authorization: `Bearer ${connection.value}`,
        },
      },
    )

    if (!acmeResponse.ok) {
      throw new Error("Failed to register webhook")
    }

    await deleteWebhookHandler(webhookHandler.id)
  }
}
```
