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.

FieldValue
Key ClassesBaseMessage, CometChatException
Key MethodsCometChat.deleteMessage()
Listener EventsonMessageDeleted
PrerequisitesSDK initialized, user logged in
// Delete a message by ID
int messageId = 1234;
await CometChat.deleteMessage(messageId,
  onSuccess: (BaseMessage message) {
    debugPrint("Message deleted at: ${message.deletedAt}");
  },
  onError: (CometChatException e) {
    debugPrint("Delete failed: ${e.message}");
  },
);

// Listen for deleted messages
CometChat.addMessageListener("listener_id", MessageListener(
  onMessageDeleted: (BaseMessage message) {
    debugPrint("Message ${message.id} was deleted");
  },
));

// Remove listener when done
CometChat.removeMessageListener("listener_id");
Who can delete: Message sender, Group admin, Group moderator Deleted fields: deletedAt (timestamp), deletedBy (user who deleted)
This operation is irreversible. Deleted messages cannot be recovered.
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 when your app was not running.
Available via: SDK | REST API | UI Kits

Delete a Message

In other words, as a sender, how do I delete a message? Use deleteMessage() with the message ID of the message to be deleted.
ParameterTypeDescription
messageIdintThe ID of the message to delete.
onSuccessFunction(BaseMessage)Callback triggered on success with the deleted message object.
onErrorFunction(CometChatException)Callback triggered on error with exception details.
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}");
  }
);
On Success — A BaseMessage object with deletedAt and deletedBy fields set:BaseMessage Object:
ParameterTypeDescriptionSample Value
idnumberUnique message ID1234
metadataobjectCustom metadata attached to the message{}
receiverobjectReceiver user objectSee below ↓
editedBystringUID of the user who edited the messagenull
conversationIdstringUnique conversation identifier"cometchat-uid-1_user_cometchat-uid-2"
sentAtnumberEpoch timestamp when the message was sent1745554729
receiverUidstringUID of the receiver"cometchat-uid-2"
typestringType of the message"text"
readAtnumberEpoch timestamp when the message was read0
deletedBystringUID of the user who deleted the message"cometchat-uid-1"
deliveredAtnumberEpoch timestamp when the message was delivered1745554750
deletedAtnumberEpoch timestamp when the message was deleted1745554800
replyCountnumberNumber of replies to this message0
senderobjectSender user objectSee below ↓
receiverTypestringType of the receiver"user"
editedAtnumberEpoch timestamp when the message was edited0
parentMessageIdnumberID of the parent message (for threads)0
readByMeAtnumberEpoch timestamp when read by the current user0
categorystringMessage category"message"
deliveredToMeAtnumberEpoch timestamp when delivered to the current user0
updatedAtnumberEpoch timestamp when the message was last updated1745554800
unreadRepliesCountnumberCount of unread replies0
quotedMessageIdnumberID of the quoted messagenull

sender Object:
ParameterTypeDescriptionSample Value
uidstringUnique identifier of the sender"cometchat-uid-1"
namestringDisplay name of the sender"Andrew Joseph"
linkstringProfile linknull
avatarstringAvatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-1.webp"
metadataobjectCustom metadata{}
statusstringOnline status"online"
rolestringUser role"default"
statusMessagestringStatus messagenull
tagsarrayUser tags[]
hasBlockedMebooleanWhether this user has blocked the current userfalse
blockedByMebooleanWhether the current user has blocked this userfalse
lastActiveAtnumberEpoch timestamp of last activity1745554700

receiver Object:
ParameterTypeDescriptionSample Value
uidstringUnique identifier of the receiver"cometchat-uid-2"
namestringDisplay name of the receiver"George Alan"
linkstringProfile linknull
avatarstringAvatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp"
metadataobjectCustom metadata{}
statusstringOnline status"offline"
rolestringUser role"default"
statusMessagestringStatus messagenull
tagsarrayUser tags[]
hasBlockedMebooleanWhether this user has blocked the current userfalse
blockedByMebooleanWhether the current user has blocked this userfalse
lastActiveAtnumberEpoch timestamp of last activity1745550000
ParameterTypeDescriptionSample Value
codestringError code identifier"ERR_CHAT_API_FAILURE"
messagestringHuman-readable error message"The message could not be deleted."
detailsstringAdditional technical details"Ensure the message ID is valid and you have permission to delete this message."
The deleted message object is returned with deletedAt (timestamp) and deletedBy (UID of deleter) fields set. Relevant fields to access on the returned message:
FieldPropertyReturn TypeDescription
deletedAtdeletedAtintTimestamp when the message was deleted
deletedBydeletedByStringUID of the user who deleted the message
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

In other words, as a recipient, how do I know when someone deletes a message when my app is running? Use onMessageDeleted in MessageListener to receive real-time delete events.
class Class_Name  with MessageListener {

//CometChat.addMessageListener("listenerId", this);
@override
void onMessageDeleted(BaseMessage message) {
	// TODO implement onMessageDeleted
}  


}   
Always remove message listeners when they’re no longer needed (e.g., in the dispose() method). Failing to remove listeners can cause memory leaks and duplicate event handling.
@override
void dispose() {
  CometChat.removeMessageListener("listenerId");
  super.dispose();
}
The onMessageDeleted callback receives a BaseMessage object with the deletedAt and deletedBy fields set. Relevant fields to access on the returned message:
FieldPropertyReturn TypeDescription
deletedAtdeletedAtintTimestamp when the message was deleted
deletedBydeletedByStringUID of the user who deleted the message

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.
You must be the message sender or a group admin/moderator to delete a message.

Next Steps

Edit Message

Modify sent messages before deletion

Send Message

Send text, media, and custom messages

Receive Messages

Handle incoming messages in real-time

Flag a Message

Report inappropriate messages