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 deletes when your app was not running.
Delete a Message
In other words, as a sender, 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.
- JavaScript
- TypeScript
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 have sent. |
| Message Sender | Group Conversation | Messages they have sent. |
| Group Admin | Group Conversation | All the messages in the group. |
| Group Moderator | Group Conversation | All the 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? In order to receive real-time events for a message being deleted, you need to override theonMessageDeleted() method of the MessageListener class.
- JavaScript
- TypeScript
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, thedeletedAt and the deletedBy fields will be set. Also, for example, of 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-
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 edit or 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
Best Practices
- Check
deletedAtwhen rendering messages. Before displaying a message, check ifdeletedAtis set. If it is, show a “This message was deleted” placeholder instead of the original content. - Use unique listener IDs. Use a unique identifier per screen or component (e.g.,
"ChatScreen_DeleteListener") to avoid conflicts with other listeners. - Always clean up listeners. Remove message listeners in your component’s cleanup or unmount lifecycle to prevent memory leaks.
- Handle permissions gracefully. If a user attempts to delete a message they don’t have permission to delete, handle the error callback and inform the user.
Troubleshooting
Troubleshooting
- “Message delete failed” error: Verify that the message ID is valid and that the current user has permission to delete the message (sender, group admin, or group moderator).
onMessageDeletednot firing: Ensure you have registered theMessageListenerwithCometChat.addMessageListener()before the delete event occurs, and that the listener ID is unique.- Deleted messages still showing in the UI: Make sure you are checking the
deletedAtfield on each message when rendering your message list, and updating your local state whenonMessageDeletedfires. - Duplicate delete events: This usually happens when multiple listeners are registered with the same or different IDs without being removed. Ensure you remove listeners on component unmount.