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);
}
);
On SuccessdeleteMessage() returns the deleted message object with deletedAt and deletedBy fields set:Message Object:
ParameterTypeDescriptionSample Value
idstringUnique message identifier"25305"
conversationIdstringConversation identifier"cometchat-uid-6_user_cometchat-uid-7"
receiverIdstringReceiver’s UID"cometchat-uid-6"
receiverTypestringType of receiver"user"
typestringMessage type"text"
categorystringMessage category"message"
sentAtnumberUnix timestamp when originally sent1771996215
deliveredAtnumberUnix timestamp when delivered1771996215
readAtnumberUnix timestamp when read1771996215
deletedAtnumberUnix timestamp when deleted1771996222
deletedBystringUID of user who deleted the message"cometchat-uid-7"
updatedAtnumberUnix timestamp when updated1771996222
senderobjectSender user detailsSee below ↓
receiverobjectReceiver user detailsSee below ↓
dataobjectAdditional message dataSee below ↓
reactionsarrayMessage reactions[]
mentionedUsersarrayUsers mentioned in message[]
mentionedMebooleanWhether current user is mentionedfalse

sender Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-7"
namestringUser’s display name"Henry Marino"
avatarstringURL to user’s avatar"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-7.webp"
statusstringUser’s online status"offline"
rolestringUser’s role"default"
lastActiveAtnumberLast active timestamp1771993054
hasBlockedMebooleanWhether user has blocked current userfalse
blockedByMebooleanWhether current user blocked this userfalse
deactivatedAtnumberDeactivation timestamp (0 if active)0
tagsarrayUser tags[]

receiver Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-6"
namestringUser’s display name"Ronald Jerry"
avatarstringURL to user’s avatar"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-6.webp"
statusstringUser’s online status"offline"
rolestringUser’s role"default"
lastActiveAtnumberLast active timestamp1771994056
hasBlockedMebooleanWhether user has blocked current userfalse
blockedByMebooleanWhether current user blocked this userfalse
deactivatedAtnumberDeactivation timestamp (0 if active)0
tagsarrayUser tags[]

data Object:
ParameterTypeDescriptionSample Value
entitiesobjectSender and receiver entitiesSee below ↓

data.entities Object:
ParameterTypeDescriptionSample Value
senderobjectSender entity wrapperSee below ↓
receiverobjectReceiver entity wrapperSee below ↓

data.entities.sender Object:
ParameterTypeDescriptionSample Value
entityTypestringType of entity"user"
entityobjectUser entity detailsSee below ↓

data.entities.sender.entity Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-7"
namestringUser’s display name"Henry Marino"
avatarstringUser’s avatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-7.webp"
rolestringUser’s role"default"
statusstringUser’s online status"offline"
lastActiveAtnumberLast active timestamp1771993054
tagsarrayUser tags[]

data.entities.receiver Object:
ParameterTypeDescriptionSample Value
entityTypestringType of entity"user"
entityobjectUser entity detailsSee below ↓

data.entities.receiver.entity Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-6"
namestringUser’s display name"Ronald Jerry"
avatarstringUser’s avatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-6.webp"
rolestringUser’s role"default"
statusstringUser’s online status"offline"
lastActiveAtnumberLast active timestamp1771994056
conversationIdstringConversation identifier"cometchat-uid-6_user_cometchat-uid-7"
tagsarrayUser tags[]
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);
  }
})
);
On EventonMessageDeleted callback receives the deleted message object:Message Object:
ParameterTypeDescriptionSample Value
idstringUnique message identifier"25301"
conversationIdstringConversation identifier"cometchat-uid-6_user_cometchat-uid-7"
receiverIdstringReceiver’s UID"cometchat-uid-7"
receiverTypestringType of receiver"user"
typestringMessage type"text"
categorystringMessage category"message"
sentAtnumberUnix timestamp when originally sent1771996032
deliveredAtnumberUnix timestamp when delivered1771996033
readAtnumberUnix timestamp when read1771996033
deletedAtnumberUnix timestamp when deleted1771996040
deletedBystringUID of user who deleted the message"cometchat-uid-6"
updatedAtnumberUnix timestamp when updated1771996040
senderobjectSender user detailsSee below ↓
receiverobjectReceiver user detailsSee below ↓
dataobjectAdditional message dataSee below ↓
reactionsarrayMessage reactions[]
mentionedUsersarrayUsers mentioned in message[]
mentionedMebooleanWhether current user is mentionedfalse

sender Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-6"
namestringUser’s display name"Ronald Jerry"
avatarstringURL to user’s avatar"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-6.webp"
statusstringUser’s online status"offline"
rolestringUser’s role"default"
lastActiveAtnumberLast active timestamp1771994056
hasBlockedMebooleanWhether user has blocked current userfalse
blockedByMebooleanWhether current user blocked this userfalse
deactivatedAtnumberDeactivation timestamp (0 if active)0
tagsarrayUser tags[]

receiver Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-7"
namestringUser’s display name"Henry Marino"
avatarstringURL to user’s avatar"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-7.webp"
statusstringUser’s online status"offline"
rolestringUser’s role"default"
lastActiveAtnumberLast active timestamp1771993054
hasBlockedMebooleanWhether user has blocked current userfalse
blockedByMebooleanWhether current user blocked this userfalse
deactivatedAtnumberDeactivation timestamp (0 if active)0
tagsarrayUser tags[]

data Object:
ParameterTypeDescriptionSample Value
entitiesobjectSender and receiver entitiesSee below ↓

data.entities Object:
ParameterTypeDescriptionSample Value
senderobjectSender entity wrapperSee below ↓
receiverobjectReceiver entity wrapperSee below ↓

data.entities.sender Object:
ParameterTypeDescriptionSample Value
entityTypestringType of entity"user"
entityobjectUser entity detailsSee below ↓

data.entities.sender.entity Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-6"
namestringUser’s display name"Ronald Jerry"
avatarstringUser’s avatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-6.webp"
rolestringUser’s role"default"
statusstringUser’s online status"offline"
lastActiveAtnumberLast active timestamp1771994056
tagsarrayUser tags[]

data.entities.receiver Object:
ParameterTypeDescriptionSample Value
entityTypestringType of entity"user"
entityobjectUser entity detailsSee below ↓

data.entities.receiver.entity Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-7"
namestringUser’s display name"Henry Marino"
avatarstringUser’s avatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-7.webp"
rolestringUser’s role"default"
statusstringUser’s online status"offline"
lastActiveAtnumberLast active timestamp1771993054
conversationIdstringConversation identifier"cometchat-uid-6_user_cometchat-uid-7"
tagsarrayUser tags[]
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.
When fetching message history — deleted messages have deletedAt and deletedBy fields set:Message Object (per item in array):
ParameterTypeDescriptionSample Value
idstringUnique message identifier"25301"
conversationIdstringConversation identifier"cometchat-uid-6_user_cometchat-uid-7"
receiverIdstringReceiver’s UID"cometchat-uid-7"
receiverTypestringType of receiver"user"
typestringMessage type"text"
categorystringMessage category"message"
sentAtnumberUnix timestamp when originally sent1771996032
deliveredAtnumberUnix timestamp when delivered1771996033
readAtnumberUnix timestamp when read1771996033
deletedAtnumberUnix timestamp when deleted1771996040
deletedBystringUID of user who deleted the message"cometchat-uid-6"
updatedAtnumberUnix timestamp when updated1771996040
senderobjectSender user detailsSee below ↓
receiverobjectReceiver user detailsSee below ↓
dataobjectAdditional message dataSee below ↓
reactionsarrayMessage reactions[]
mentionedUsersarrayUsers mentioned in message[]
mentionedMebooleanWhether current user is mentionedfalse

sender Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-6"
namestringUser’s display name"Ronald Jerry"
avatarstringURL to user’s avatar"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-6.webp"
statusstringUser’s online status"offline"
rolestringUser’s role"default"
lastActiveAtnumberLast active timestamp1771994056
hasBlockedMebooleanWhether user has blocked current userfalse
blockedByMebooleanWhether current user blocked this userfalse
deactivatedAtnumberDeactivation timestamp (0 if active)0
tagsarrayUser tags[]

receiver Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-7"
namestringUser’s display name"Henry Marino"
avatarstringURL to user’s avatar"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-7.webp"
statusstringUser’s online status"offline"
rolestringUser’s role"default"
lastActiveAtnumberLast active timestamp1771993054
hasBlockedMebooleanWhether user has blocked current userfalse
blockedByMebooleanWhether current user blocked this userfalse
deactivatedAtnumberDeactivation timestamp (0 if active)0
tagsarrayUser tags[]

data Object:
ParameterTypeDescriptionSample Value
entitiesobjectSender and receiver entitiesSee below ↓

data.entities Object:
ParameterTypeDescriptionSample Value
senderobjectSender entity wrapperSee below ↓
receiverobjectReceiver entity wrapperSee below ↓

data.entities.sender Object:
ParameterTypeDescriptionSample Value
entityTypestringType of entity"user"
entityobjectUser entity detailsSee below ↓

data.entities.sender.entity Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-6"
namestringUser’s display name"Ronald Jerry"
avatarstringUser’s avatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-6.webp"
rolestringUser’s role"default"
statusstringUser’s online status"offline"
lastActiveAtnumberLast active timestamp1771994056
tagsarrayUser tags[]

data.entities.receiver Object:
ParameterTypeDescriptionSample Value
entityTypestringType of entity"user"
entityobjectUser entity detailsSee below ↓

data.entities.receiver.entity Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-7"
namestringUser’s display name"Henry Marino"
avatarstringUser’s avatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-7.webp"
rolestringUser’s role"default"
statusstringUser’s online status"offline"
lastActiveAtnumberLast active timestamp1771993054
conversationIdstringConversation identifier"cometchat-uid-6_user_cometchat-uid-7"
tagsarrayUser tags[]
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.
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.
  • 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