> ## Documentation Index
> Fetch the complete documentation index at: https://www.cometchat.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Delete A Message

> Delete CometChat messages in Flutter apps and handle real-time or missed message deletion events.

> [accordion] AI Integration Quick Reference
>
> | Field | Value |
> | - | - |
> | Package | `cometchat_sdk` |
> | Import | `import 'package:cometchat_sdk/cometchat_sdk.dart';` |
> | Purpose | Delete CometChat messages in Flutter apps and handle real-time or missed message deletion events. |
> | Key methods | `addMessageListener()` · `deleteMessage()` |
> | Key classes | `BaseMessage` · `CometChatException` |
> | Listener callbacks | `onMessageDeleted()` |
> | Prerequisites | SDK initialised via [`CometChat.init()`](https://www.cometchat.com/docs/sdk/flutter/setup) and a logged-in user via [`CometChat.login()`](https://www.cometchat.com/docs/sdk/flutter/authentication-overview). |
> | Constraints | `onSuccess` and `onError` are **both required** — omitting either is a compile error, and awaiting the call alone gives you nothing to act on. Put the success path inside `onSuccess`. |
> | Related | [Send A Message](https://www.cometchat.com/docs/sdk/flutter/send-message.md) · [Edit A Message](https://www.cometchat.com/docs/sdk/flutter/edit-message.md) |
> | Full reference | [`BaseMessage`](https://www.cometchat.com/docs/sdk/reference/messages#basemessage) · [`CometChatException`](https://www.cometchat.com/docs/sdk/reference/auxiliary#cometchatexception) |

While [deleting a message](https://www.cometchat.com/docs/sdk/flutter/delete-message.md#delete-a-message) is straightforward, receiving events for deleted messages with CometChat has two parts:

1. Adding a listener to receive [real-time message deletes](https://www.cometchat.com/docs/sdk/flutter/delete-message.md#real-time-message-delete-events) when your app is running.
2. Calling a method to retrieve [missed message delete events](https://www.cometchat.com/docs/sdk/flutter/delete-message.md#missed-message-delete-events)-me when your app was not running.

## Delete a Message

*In other words, as a sender, how do I delete a message?*

In case you have to delete a message, you can use the `deleteMessage()` method. This method takes the message ID of the message to be deleted.

> [!NOTE]
>
> Deleting a message needs a connection — with offline support enabled, `deleteMessage()` is **not** queued for later. After `onSuccess`, the deletion is applied to the message in the on-device cache if it is already cached; it never adds a new message. See [Send While Offline](https://www.cometchat.com/docs/sdk/flutter/offline-send.md).

> [tab] Delete Message
>
> ```dart theme={null}
> int messageId=1234;
>
> await CometChat.deleteMessage(messageId, 
>   onSuccess: (BaseMessage message){
>   	debugPrint("Message deleted successfully at : $message");
>   }, onError: (CometChatException e){
>   	debugPrint("Message deletion failed : ${e.message}");
>   }
> );
> ```

Once the message is deleted, In the `onSuccess()` callback, you get an object of the `BaseMessage` class, with the `deletedAt` field set with the timestamp of the time the message was deleted. Also, the `deletedBy` field is set. These two fields can be used to identify if the message is deleted while iterating through a list of messages.

By default, CometChat allows certain roles to delete a message.

## Real-time Message Delete Events

*In other words, as a recipient, how do I know when someone deletes a message when my app is running?*

| User Role | Conversation Type | Deletion Capabilities |
| - | - | - |
| Message Sender | One-on-one Conversation | Messages they've sent |
| Message Sender | Group Conversation | Messages they've sent |
| Group Admin | Group Conversation | All messages in the group |
| Group Moderator | Group Conversation | All messages in the group |

In order to receive real-time events for a message being deleted, you need to override the `onMessageDeleted()` method of the `MessageListener` class.

> [tab] Dart
>
> ```dart theme={null}
> class Class_Name  with MessageListener {
>
> //CometChat.addMessageListener("listenerId", this);
> @override
> void onMessageDeleted(BaseMessage message) {
> 	// TODO implement onMessageDeleted
> }  
>
>
> }   
> ```

## Missed Message Delete Events

*In other words, as a recipient, how do I know if someone deleted a message when my app was not running?*

When you retrieve the list of previous messages, for the messages that were deleted, the `deletedAt` and the `deletedBy` fields will be set. Also, for example, if the total number of messages for a conversation are 100, and the message with message ID 50 was deleted. Now the message with ID 50 will have the `deletedAt` and the `deletedBy` fields set whenever it is pulled from the history. Also, the 101st message will be an `Action` message informing you that the message with ID 50 has been deleted.

For the message deleted event, in the `Action` object received, the following fields can help you get the relevant information-

1. `action` - `deleted`
2. `actionOn` - Updated message object which was deleted.
3. `actionBy` - User object containing the details of the user who has deleted the message.
4. `actionFor` - User/group object having the details of the receiver to which the message was sent.

> [!NOTE]
>
> In order to delete a message, you need to be either the sender of the message or the admin/moderator of the group in which the message was sent.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.