> 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/python.md). Agents can also search and read this documentation through its [MCP server](https://dnotifier.gitbook.io/product-docs/~gitbook/mcp).

# Python

Delete persisted messages with `delete_chat_history_message`.

***

## Basic delete

```python
from dnotifier import DNotifier

MY_USER_ID = "user-alice"

async def delete_message(notifier: DNotifier, message_id: str):
    await notifier.delete_chat_history_message(
        sender_id=MY_USER_ID,
        message_id=message_id,
    )
    remove_from_local_state(message_id)
```

***

## Delete from message list UI

```python
async def handle_delete(notifier: DNotifier, message):
    if not message.id:
        return
    try:
        await delete_message(notifier, message.id)
    except Exception as e:
        print(f"Delete failed: {e}")
```

***

## Notify peer (optional)

```python
async def delete_and_notify_peer(notifier: DNotifier, message_id: str, peer_id: str):
    await notifier.delete_chat_history_message(
        sender_id=MY_USER_ID,
        message_id=message_id,
    )

    await notifier.send(
        sender_id=MY_USER_ID,
        receiver_id=peer_id,
        data={"type": "message-deleted", "messageId": message_id},
        save_history=False,
    )

    remove_from_local_state(message_id)
```

Handle on the peer:

```python
body = msg.payload.to_json()
if isinstance(body, dict) and body.get("type") == "message-deleted":
    remove_from_local_state(body.get("messageId"))
```

***

## Troubleshooting

| Issue                         | Check                                         |
| ----------------------------- | --------------------------------------------- |
| Missing message\_id           | Pass id from `metadata.id` or history         |
| `RuntimeError: Not connected` | Reconnect before delete                       |
| Message still in UI           | Update local state; refetch history if needed |

***

## Learn more

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