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
MessagesRequestwith appropriate filters - Related: Edit Message · Send Message · Messaging Overview
- 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? In case you have to delete a message, you can use thedeleteMessage() method. This method takes the message ID of the message to be deleted.
- Swift
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.Sample Payloads
Sample Payloads
- Request
- Success Response
- Error Response
Method:
CometChat.delete(messageId:)| Parameter | Type | Value |
|---|---|---|
| messageId | Int | 12345 |
| User Role | Conversation Type | Deletion Capabilities |
|---|---|---|
| Message Sender | One-on-one | Own messages only |
| Message Sender | Group | Own messages only |
| Group Admin | Group | All messages |
| Group Moderator | Group | All messages |
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, implementCometChatMessageDelegate:
- Swift
Sample Payloads - onMessageDeleted
Sample Payloads - onMessageDeleted
- Callback Payload
Method:
onMessageDeleted(message:)Object Type: BaseMessage| Parameter | Type | Description |
|---|---|---|
| message | BaseMessage | The deleted message object |
| message.id | Int | Message ID |
| message.deletedAt | Double | Unix timestamp when deleted |
| message.deletedBy | String? | UID of user who deleted |
| message.text | String | Original text (still available) |
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, 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:
- Deleted Message in History: The message object has
deletedAtanddeletedByfields set. Original content is still available (soft delete). - Action Message: An
ActionMessageis added to history indicating the deletion.
- Swift
Sample Payloads - Action Message for Delete
Sample Payloads - Action Message for Delete
- Action Message Payload
Object Type: ActionMessage
| Parameter | Type | Description |
|---|---|---|
| action | String | "deleted" |
| actionOn | BaseMessage | The deleted message object |
| actionBy | User | User who deleted the message |
| actionFor | AppEntity | Receiver (User or Group) |
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
Whendelete() is successful, the onSuccess callback returns the deleted BaseMessage object:
- Swift
Delete Message Failure Response
Whendelete() fails, the onError callback returns a CometChatException:
- Swift
Deleted Message Properties
When a message is deleted, these properties are set:| Property | Type | Description |
|---|---|---|
deletedAt | Double | Unix timestamp when message was deleted |
deletedBy | String? | UID of user who deleted the message |
updatedAt | Double | Same as deletedAt for deleted messages |
Common Error Codes
| Error Code | Description | Resolution |
|---|---|---|
ERR_NOT_LOGGED_IN | User is not logged in | Login first using CometChat.login() |
ERR_MESSAGE_NOT_FOUND | Message does not exist | Verify the message ID is correct |
ERR_PERMISSION_DENIED | No permission to delete | Only sender or group admin/moderator can delete |
ERR_MESSAGE_ALREADY_DELETED | Message already deleted | No action needed |