Skip to main content
Quick Reference for AI Agents & Developers
  • Delete message: CometChat.delete(messageId:onSuccess:onError:)
  • Behavior: Soft delete — message marked as deleted, not removed from server
  • Listen for deletions: onMessageDeleted(_:) in message listener delegate
  • Missed deletions: Use MessagesRequest with appropriate filters
  • Related: Edit Message · Send Message · Messaging Overview
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.

Delete a Message

In other words, as a sender, how do I delete a message? In case you have to delete a message, you can use the deleteMessage() method. This method takes the message ID of the message to be deleted.
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.
This is a SOFT DELETE - message content is still available. Check deletedAt > 0 to identify deleted messages.
Method: CometChat.delete(messageId:)
By default, CometChat allows certain roles to delete a message.

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? To receive real-time delete events, implement CometChatMessageDelegate:
Method: onMessageDeleted(message:)Object Type: BaseMessage

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, 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 your app was not running, deleted messages appear in two ways:
  1. Deleted Message in History: The message object has deletedAt and deletedBy fields set. Original content is still available (soft delete).
  2. Action Message: An ActionMessage is added to history indicating the deletion.
Object Type: ActionMessage
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.

Success & Failure Responses

Delete Message Success Response

When delete() is successful, the onSuccess callback returns the deleted BaseMessage object:

Delete Message Failure Response

When delete() fails, the onError callback returns a CometChatException:

Deleted Message Properties

When a message is deleted, these properties are set:

Common Error Codes