Skip to main content
Quick Reference for AI Agents & Developers
// 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?) { }
})
Available via: SDK | REST API | UI Kits
While deleting a message is straightforward, receiving events for deleted messages with CometChat has two parts:
  1. Adding a listener to receive real-time message deletes when your app is running.
  2. Calling a method to retrieve missed message delete events-me when your app was not running.

Delete a Message

How do I delete a message? To delete a message, use the deleteMessage() method. This method takes the message ID of the message to be deleted. For more details on message properties, see Message Structure & Hierarchy.
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());
  }
});
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.
User RoleConversation TypeDeletion Capabilities
Message SenderOne-on-one ConversationMessages they’ve sent
Message SenderGroup ConversationMessages they’ve sent
Group AdminGroup ConversationAll messages in the group
Group ModeratorGroup ConversationAll messages in the group

Real-time Message Delete Events

How do I know when someone deletes a message while my app is running? To receive real-time events for a message being deleted, override the onMessageDeleted() method of the MessageListener class. For more information on message listeners, see Real-Time Listeners.
CometChat.addMessageListener(listenerID, new CometChat.MessageListener() {
  @Override
  public void onMessageDeleted(BaseMessage message) {
    Log.d(TAG, "Message Edited");
  }
});
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.
CometChat.removeMessageListener("LISTENER_ID")

Missed Message Delete Events

How do I know if someone deleted a message while 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. For example, if the total number of messages for a conversation is 100, and the message with message ID 50 was deleted, the message with ID 50 will have the deletedAt and the deletedBy fields set whenever it is pulled from the history. Additionally, 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.
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.

Best Practices

Check user roles before allowing deletions in your UI. Only message senders, group admins, and group moderators can delete messages to prevent unauthorized deletions.
Use the deletedAt and deletedBy fields to identify deleted messages when displaying message history. Consider showing a placeholder like “This message was deleted” instead of removing the message entirely.
When a message is deleted, the message object remains in history with deletedAt set. This preserves conversation flow and allows users to see that a message existed but was removed.

Troubleshooting

Symptom: deleteMessage() fails with “Permission denied” error.Cause: User attempting to delete a message they didn’t send, or lacking admin/moderator privileges in a group.Solution: Verify the logged-in user is either the message sender or has admin/moderator role in the group. Use message.getSender().getUid() to check message ownership before allowing deletion.
Symptom: deleteMessage() fails with “Message not found” error.Cause: The message ID provided doesn’t exist or the message was already deleted.Solution: Verify the message ID is correct and the message exists. Check if message.getDeletedAt() is already set, indicating the message was previously deleted.
Symptom: Deleted messages continue to show in the message list.Cause: UI not properly handling the onMessageDeleted() callback or not checking the deletedAt field when rendering messages.Solution: Implement the onMessageDeleted() listener to update your UI in real-time. When fetching message history, check if message.getDeletedAt() is set and render accordingly.

Next Steps