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

# Dart / Flutter

Delete persisted messages with `deleteChatHistoryMessage`.

***

## Basic delete

```dart
import 'package:dnotifier/dnotifier.dart';

const myUserId = 'user-alice';

Future<void> deleteMessage(DNotifier notifier, String messageId) async {
  await notifier.deleteChatHistoryMessage(
    senderId: myUserId,
    messageId: messageId,
  );
  removeFromLocalState(messageId);
}
```

***

## Delete from message list UI

```dart
ListTile(
  title: Text(message.text),
  trailing: IconButton(
    icon: const Icon(Icons.delete),
    onPressed: () async {
      if (message.id == null) return;
      try {
        await deleteMessage(notifier, message.id!);
      } catch (e) {
        showSnackBar('Delete failed: $e');
      }
    },
  ),
)
```

***

## Notify peer (optional)

```dart
Future<void> deleteAndNotifyPeer(
  DNotifier notifier,
  String messageId,
  String peerId,
) async {
  await notifier.deleteChatHistoryMessage(
    senderId: myUserId,
    messageId: messageId,
  );

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

  removeFromLocalState(messageId);
}
```

Handle on the peer:

```dart
if (body is Map && body['type'] == 'message-deleted') {
  removeFromLocalState(body['messageId']?.toString());
}
```

***

## Troubleshooting

| Issue                               | Check                                         |
| ----------------------------------- | --------------------------------------------- |
| `ArgumentError: messageId required` | Pass id from `metadata.id` or history         |
| `StateError: 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/dart-flutter.md)
* [Save history flag](https://dnotifier.gitbook.io/product-docs/chat/save-history.md)
