> For the complete documentation index, see [llms.txt](https://dnotifier.gitbook.io/product-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dnotifier.gitbook.io/product-docs/chat/delete-messages/javascript-typescript.md). Agents can also search and read this documentation through its [MCP server](https://dnotifier.gitbook.io/product-docs/~gitbook/mcp).

# JavaScript / TypeScript

Delete persisted messages with `deleteChatHistoryMessage`.

***

## Basic delete

```js
import WebSocket from "ws";
import { DNotifier } from "@dnotifier-realtime/dnotifier";

const MY_USER_ID = "user-alice";

const notifier = new DNotifier({
  appId: process.env.DNOTIFIER_APP_ID,
  secret: process.env.DNOTIFIER_SECRET,
  transport: "ws",
  userId: MY_USER_ID,
  WebSocketImpl: WebSocket,
  onConnected: () => {},
  onMessage: (msg) => {
    const body = msg.payload.toJSON();
    // store msg.metadata.id with each rendered message
  },
  onDisconnected: () => {},
});

await notifier.connect();

async function deleteMessage(messageId) {
  await notifier.deleteChatHistoryMessage({
    senderId: MY_USER_ID,
    messageId,
  });
  removeFromLocalState(messageId);
}
```

***

## Delete from message list UI

```js
function onDeleteClick(message) {
  if (!message.id) return;

  deleteMessage(message.id).catch((err) => {
    showToast(`Delete failed: ${err.message}`);
  });
}
```

***

## Notify peer (optional)

```js
async function deleteAndNotifyPeer(messageId, peerId) {
  await notifier.deleteChatHistoryMessage({
    senderId: MY_USER_ID,
    messageId,
  });

  await notifier.send({
    senderId: MY_USER_ID,
    receiverId: peerId,
    data: { type: "message-deleted", messageId },
    saveHistory: false,
  });

  removeFromLocalState(messageId);
}
```

Handle on the peer:

```js
if (body?.type === "message-deleted") {
  removeFromLocalState(body.messageId);
}
```

***

## Troubleshooting

| Issue                            | Check                                         |
| -------------------------------- | --------------------------------------------- |
| `messageId required`             | Pass the id from `metadata.id` or history     |
| `Not connected`                  | Reconnect before delete                       |
| Message still in UI after delete | Update local state; refetch history if needed |

***

## Learn more

* [Chat history](https://dnotifier.gitbook.io/product-docs/chat/chat-history/javascript-typescript.md)
* [Save history flag](https://dnotifier.gitbook.io/product-docs/chat/save-history.md)
