Skip to main content
Quick Reference for AI Agents & Developers
// Delete a message by ID
let messageId = "MESSAGE_ID";
CometChat.deleteMessage(messageId).then(
  (message) => console.log("Message deleted", message),
  (error) => console.log("Delete failed:", error)
);

// Listen for real-time message deletes
CometChat.addMessageListener("LISTENER_ID", new CometChat.MessageListener({
  onMessageDeleted: (message) => console.log("Deleted:", message)
}));
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 deletes 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? 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.
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);
}
);
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 have sent.
Message SenderGroup ConversationMessages they have sent.
Group AdminGroup ConversationAll the messages in the group.
Group ModeratorGroup ConversationAll 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 the onMessageDeleted() method of the MessageListener class.
var listenerID = "UNIQUE_LISTENER_ID";

CometChat.addMessageListener(
listenerID,
new CometChat.MessageListener({
  onMessageDeleted: message => {
    console.log("Deleted Message", message);
  }
})
);
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.
CometChat.removeMessageListener("UNIQUE_LISTENER_ID");

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, 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-
  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 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.
  • 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.
  • “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.

Next Steps