Skip to main content

Documentation Index

Fetch the complete documentation index at: https://cometchat-22654f5b-docs-android-v6-beta2.mintlify.app/llms.txt

Use this file to discover all available pages before exploring further.

// 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 when your app is running
  2. Fetching missed deletes when your app was offline

Delete a Message

Use deleteMessage() with the message ID.
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());
  }
});
FieldGetterReturn TypeDescription
deletedAtgetDeletedAt()longTimestamp when the message was deleted
deletedBygetDeletedBy()StringUID of the user who deleted the 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.
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

Use onMessageDeleted in MessageListener to receive real-time delete events.
CometChat.addMessageListener(listenerID, new CometChat.MessageListener() {
  @Override
  public void onMessageDeleted(BaseMessage message) {
    Log.d(TAG, "Message Edited");
  }
});
FieldGetterReturn TypeDescription
deletedAtgetDeletedAt()longTimestamp when the message was deleted
deletedBygetDeletedBy()StringUID of the user who deleted the message
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

When fetching message history, deleted messages have deletedAt and deletedBy fields set. Additionally, an Action message is created when a message is 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.


Next Steps

Edit Message

Modify text and custom messages after sending

Send Message

Send text, media, and custom messages

Receive Messages

Handle real-time message events with listeners

Message Structure

Understand message types and properties