# mergeUser()

Merges the `fromUserId` user into the given user: moves its identifications, connected accounts, second factors, organization memberships, and metadata onto the given user, then deletes the `fromUserId` user. Where both users have a value, the given user keeps its own.

A merge cannot be undone. Use `dryRun` to preview one.

Returns the remaining [`User`](https://clerk.com/docs/reference/backend/types/backend-user.md) after the merge, and the deleted user as it was before the merge.

```typescript
function mergeUser(userId: string, params: { dryRun?: boolean; fromUserId: string }): Promise<UserMergeResult>
```

## Parameters

| Parameter           | Type                                                   | Description                                                                               |
| ------------------- | ------------------------------------------------------ | ----------------------------------------------------------------------------------------- |
| `userId`            | `string`                                               | The ID of the user that remains after the merge.                                          |
| `params`            | `{ dryRun?: boolean; fromUserId: string; }` | The parameters for the merge.                                                             |
| `params.dryRun?`    | `boolean`                                              | Whether to return the result of the merge without changing anything. Defaults to `false`. |
| `params.fromUserId` | `string`                                               | The ID of the user to merge into the other user. This user is deleted.                    |

## Usage

> Using `clerkClient` varies based on the SDK you're using. Refer to the [overview](https://clerk.com/docs/reference/backend/overview.md) for usage details, including guidance on [how to access the `userId` and other properties](https://clerk.com/docs/reference/backend/overview.md#example-get-the-userid-and-other-properties).

### Preview a merge

Use `dryRun` to preview the result without changing either user.

```tsx
const userId = 'user_123'
const fromUserId = 'user_456'

const preview = await clerkClient.users.mergeUser(userId, { fromUserId, dryRun: true })
```

### Merge users

Review the preview, then omit `dryRun` to merge the users. This deletes the `fromUserId` user and revokes its sessions.

```tsx
const userId = 'user_123'
const fromUserId = 'user_456'

const response = await clerkClient.users.mergeUser(userId, { fromUserId })
```

## Backend API (BAPI) endpoint

This method in the SDK is a wrapper around the BAPI endpoint `POST/users/{user_id}/merge`. See the [BAPI reference](https://clerk.com/docs/reference/backend-api/tag/users/POST/users/%7Buser_id%7D/merge) for more information.

---

## Sitemap

[Overview of all docs pages](https://clerk.com/docs/llms.txt)
