Quick Reference for AI Agents & Developers
- Adding a listener to receive real-time message deletes when your app is running.
- 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 thedeleteMessage() method. This method takes the message ID of the message to be deleted. For more details on message properties, see Message Structure & Hierarchy.
- Java
- Kotlin
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 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
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 theonMessageDeleted() method of the MessageListener class. For more information on message listeners, see Real-Time Listeners.
- Java
- Kotlin
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, thedeletedAt 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-
action-deletedactionOn- Updated message object which was deleted.actionBy- User object containing the details of the user who has deleted the message.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
Verify Deletion Permissions
Verify Deletion Permissions
Check user roles before allowing deletions in your UI. Only message senders, group admins, and group moderators can delete messages to prevent unauthorized deletions.
Handle Deleted Messages in UI
Handle Deleted Messages in UI
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.Preserve Message Context
Preserve Message Context
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
Delete Failed - Permission Denied
Delete Failed - Permission Denied
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.Delete Failed - Invalid Message ID
Delete Failed - Invalid Message ID
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.Deleted Messages Still Appear
Deleted Messages Still Appear
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.