> ## 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 Message

> Delete sent messages in one-on-one or group conversations using the CometChat Android SDK.

> [accordion] AI Integration Quick Reference
>
> ```kotlin theme={null}
> // Delete a message by ID
> val messageId = 1234
>
> CometChat.deleteMessage(messageId, object : CometChat.CallbackListener<BaseMessage>() {
>     override fun onSuccess(message: BaseMessage) {
>         // message.deletedAt contains deletion timestamp
>     }
>     override fun onError(e: CometChatException) { }
> })
>
> // Listen for delete events
> CometChat.addMessageListener("LISTENER_ID", object : CometChat.MessageListener() {
>     override fun onMessageDeleted(message: BaseMessage?) { }
> })
> ```

Deleting a message is straightforward. Receiving delete events has two parts:

1. Adding a listener for [real-time deletes](#real-time-message-delete-events) when your app is running
2. Fetching [missed deletes](#missed-message-delete-events) when your app was offline

## Delete a Message

Use `deleteMessage()` with the message ID.

> [tab] Java
>
> ```java theme={null}
> private int messageId = 1234;
>
> CometChat.deleteMessage(messageId, new CometChat.CallbackListener<BaseMessage>() {
>   @Override
>   public void onSuccess(BaseMessage message) {
>     Log.d(TAG, "Message deleted successfully at : " + message.getDeletedAt());
>   }
>
>   @Override
>   public void onError(CometChatException e) {
>     Log.d(TAG, e.getMessage());
>   }
> });
> ```

> [tab] Kotlin
>
> ```kotlin theme={null}
> private val messageId = 1234
>
> CometChat.deleteMessage(messageId,object : CometChat.CallbackListener<BaseMessage>(){
>   override fun onSuccess(message: BaseMessage) {
>     Log.d(TAG, "deleteMessage onSuccess : " + message.deletedAt)
>   }
>
>   override fun onError(e: CometChatException) {
>     Log.d(TAG, "deleteMessage onError : "+e.message)
>   }
> })
> ```

| Field | Getter | Return Type | Description |
| - | - | - | - |
| deletedAt | `getDeletedAt()` | `long` | Timestamp when the message was deleted |
| deletedBy | `getDeletedBy()` | `String` | UID of the user who deleted the message |

Once the message is deleted, In the `onSuccess()` callback, you get an object of the [`BaseMessage`](https://www.cometchat.com/docs/sdk/reference/messages#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.

| 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 |

## Real-time Message Delete Events

Use `onMessageDeleted` in `MessageListener` to receive real-time delete events.

> [tab] Java
>
> ```java theme={null}
> CometChat.addMessageListener(listenerID, new CometChat.MessageListener() {
>   @Override
>   public void onMessageDeleted(BaseMessage message) {
>     Log.d(TAG, "Message Edited");
>   }
> });
> ```

> [tab] Kotlin
>
> ```kotlin theme={null}
> CometChat.addMessageListener(listenerID, object : CometChat.MessageListener() {
>   override fun onMessageDeleted(message: BaseMessage?) {
>     Log.d(TAG, "Message Edited")
>   }
> })
> ```

| Field | Getter | Return Type | Description |
| - | - | - | - |
| deletedAt | `getDeletedAt()` | `long` | Timestamp when the message was deleted |
| deletedBy | `getDeletedBy()` | `String` | UID of the user who deleted the message |

> [!WARNING]
>
> Always remove listeners when they're no longer needed (e.g., in `onDestroy()` or when navigating away). Failing to remove listeners can cause memory leaks and duplicate event handling.
>
> ```kotlin theme={null}
> CometChat.removeMessageListener("LISTENER_ID")
> ```

## Missed Message Delete Events

When fetching message history, deleted messages have `deletedAt` and `deletedBy` fields set. Additionally, an [`Action`](https://www.cometchat.com/docs/sdk/reference/messages#action) message is created when a message is deleted.

For the message deleted event, in the [`Action`](https://www.cometchat.com/docs/sdk/reference/messages#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.

***

***

## Next Steps

> [card] [Edit Message](https://www.cometchat.com/docs/sdk/android/v4/edit-message.md)
>
> Modify text and custom messages after sending

> [card] [Send Message](https://www.cometchat.com/docs/sdk/android/v4/send-message.md)
>
> Send text, media, and custom messages

> [card] [Receive Messages](https://www.cometchat.com/docs/sdk/android/v4/receive-messages.md)
>
> Handle real-time message events with listeners

> [card] [Message Structure](https://www.cometchat.com/docs/sdk/android/v4/message-structure-and-hierarchy.md)
>
> Understand message types and properties


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