---
description: Similar to EventEmitter.prototype.removeListener()
---

# evt.detach\(\[ctx\]\)

Detach all handlers from the Evt or all Evt's handler that are bound to a given context.

{% hint style="info" %}
The prefered way of detaching handler in TS-EVT is using the `ctx.done()`.
{% endhint %}

{% hint style="warning" %}
Calling this method without passing a context argument is almost never what you want to do. An Evt instance should be sharable by modules that are isolated one another. If a module take the liberty to call evt.detach\(\) it can brek the code elswhere.
{% endhint %}

{% hint style="info" %}
To chery pick the handlers to detach use `evt.getHandlers()` or `ctx.getHandlers()`
{% endhint %}

## Returns

`Handler<T,any>[]` array of Handler that have been detached.

## Parameters

`ctx?: Ctx` If `Ctx` is provided only Handler bound to the given context will be removed.



## Examples

To detach all handlers at once:

```typescript
const evtText = new Evt<string>();
//detach with no argument will detach all handlers (attach, attachOnce, waitFor... )
evtText.detach();
```

Using a context argument

```typescript
import { Evt } from "ts-evt";

const evtText = new Evt<string>();

evtText.attachOnce(text=> console.log(`Hello ${text}`));

const ctx = Evt.newCtx();

evtText.attach(
    ctx,
    _text => console.assert(false,"never")
);

evtText.attachOnce(
    ctx,
    _text => console.assert(false,"never")
);

evtText.detach(boundTo);

//"Hello World" will be printed
evtText.post("World");
```

[**Run the example**](https://stackblitz.com/edit/ts-evt-demo-detach-with-contex?embed=1&file=index.ts)

