---
title: "remove"
description: "Remove a node from a tree by id and return the removed node, detaching its subtree from the parent in a single call."
canonical_url: "https://core.mhaibaraai.cn/en/docs/transformers/tree/remove"
---
# remove

> Remove a node from a tree by id and return the removed node, detaching its subtree from the parent in a single call.

## Usage

The `remove` method removes a node and all its descendants from the tree by node `id`. This method **mutates** the tree array in place.

```ts [example.ts]
import { Tree } from '@movk/core'

const tree = [{ id: 1, name: 'A', children: [{ id: 2, name: 'B' }] }]

Tree.remove(tree, 2)

/*
tree will be:
[{ id: 1, name: 'A', children: [] }]
*/
```

## API

`remove<T extends TreeNode>(tree: T[], id: number | string, config?: TreeConfig): boolean`

### Parameters

**tree** (`T[]`) *required*: The source tree array.

**id** (`number | string`) *required*: The ID of the node to remove.

**config** (`TreeConfig`): Configuration object for customizing the id, pid, children key names in the tree structure.Optional. The key name for the node's unique identifier. Defaults to "id".Optional. The key name for the node's parent identifier. Defaults to "pid".Optional. The key name for the children array. Defaults to "children".

### Returns

**boolean**: Returns true if the node was found and removed, otherwise false.

## Changelog

See commit history for [src/transformers/tree/mutate.ts](https://github.com/mhaibaraai/movk-core/commits/main/src/transformers/tree/mutate.ts).

---

- [GitHub](https://github.com/mhaibaraai/movk-core/blob/main/src/transformers/tree/mutate.ts)


## Sitemap

See the full [sitemap](https://core.mhaibaraai.cn/sitemap.md) for all pages.
