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

# useTeardown

> Clean up before app destruction.

<Badge color="yellow">sunpeak API</Badge>

## Overview

Registers a cleanup callback that runs before the app's iframe is destroyed. Use this to save state, close connections, or perform other cleanup.

<Card horizontal title="Wraps onteardown" icon="cube" href="/mcp-apps/app/event-handlers/onteardown">
  MCP Apps SDK reference
</Card>

## Import

```tsx theme={null}
import { useTeardown } from 'sunpeak';
```

## Signature

```tsx theme={null}
function useTeardown(
  callback: () => Promise<void> | void
): void
```

## Parameters

<ResponseField name="callback" type="() => Promise<void> | void" required>
  Cleanup function to run before app destruction.
</ResponseField>

## Returns

<ResponseField name="return" type="void">
  This hook does not return a value.
</ResponseField>

## Usage

```tsx theme={null}
import { useTeardown } from 'sunpeak';
import { useCallback } from 'react';

function MyResource() {
  const cleanup = useCallback(async () => {
    console.log('App is being torn down');
    // Save state, close connections, etc.
  }, []);

  useTeardown(cleanup);

  return <div>My Resource</div>;
}
```
