> ## 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 occurs when a user removes a
[connection](https://docs.attio.com/sdk/server/connections/connections.md). It also occurs when a workspace uninstalls your
app: one time for the workspace connection and one time for each user connection. 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.

## During an uninstall

When a workspace uninstalls your app, Attio removes the connection even if the handler fails:

* If the handler throws, Attio calls it again, for a maximum of 5 attempts. The minimum time
  between attempts is 30 seconds. After the last attempt, Attio removes the connection.
* If the handler runs for longer than the 30-second
  [server function timeout](https://docs.attio.com/sdk/server/server-functions.md#timeout), Attio stops it. Unlike a throw, a timeout
  is not retried: Attio removes the connection.
* In two cases, the handler cannot run, so Attio removes the connection without it and your
  cleanup does not happen:
  * The member who created the connection is not an active member of the workspace now. The
    handler runs as this member.
  * The OAuth access token of the connection is expired, and Attio cannot refresh it. The handler
    cannot use the connection.

See [Cleaning up on uninstall](https://docs.attio.com/sdk/guides/cleaning-up-on-uninstall.md).

## Example

Here's an example that cleans up a [webhook handler](https://docs.attio.com/sdk/server/webhooks/webhook-handlers.md).

```ts TypeScript theme={"system"}
// src/app/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 delete webhook")
    }

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


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.