In other words, as a sender, 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.
This operation is irreversible. Deleted messages cannot be recovered.
JavaScript
TypeScript
Report incorrect code
Copy
Ask AI
let messageId = "ID_OF_THE_MESSAGE_YOU_WANT_TO_DELETE";CometChat.deleteMessage(messageId).then(message => { console.log("Message deleted", message);}, error => { console.log("Message delete failed with error:", error);});
Report incorrect code
Copy
Ask AI
let messageId: number = 1;CometChat.deleteMessage(messageId).then( (message: CometChat.BaseMessage) => { console.log("Message deleted", message); }, (error: CometChat.CometChatException) => { console.log("Message delete failed with error:", error); });
Response
On Success — deleteMessage() returns the deleted message object with deletedAt and deletedBy fields set:Message Object:
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.
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 the onMessageDeleted() method of the MessageListener class.
Always remove listeners when they are no longer needed (e.g., on component unmount or screen navigation). Failing to remove listeners can cause memory leaks and duplicate event handling.
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, 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.
Response
When fetching message history — deleted messages have deletedAt and deletedBy fields set:Message Object (per item in array):
For the message deleted event, in the Action object received, the following fields can help you get the relevant information-
action - deleted
actionOn - 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.
When fetching message history, deleted messages include deletedAt and deletedBy fields. You can check if deletedAt is set to identify deleted messages. Additionally, an Action message with action: "deleted" is added to the message list for audit/timeline purposes.
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
Check deletedAt when rendering messages. Before displaying a message, check if deletedAt is 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
“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).
onMessageDeleted not firing: Ensure you have registered the MessageListener with CometChat.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 deletedAt field on each message when rendering your message list, and updating your local state when onMessageDeleted fires.
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.