> ## Documentation Index
> Fetch the complete documentation index at: https://cometchat-22654f5b-docs-agent-in-group-react-v6.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Delete Message

> Delete sent messages in one-on-one or group conversations with the CometChat iOS SDK and handle deletion events.

<Accordion title="AI Integration Quick Reference">
  ```swift theme={null}
  // Delete a message
  CometChat.delete(messageId: 12345, onSuccess: { msg in
    print("Deleted:", msg.id, msg.deletedAt)
  }, onError: { err in })

  // Listen for real-time deletions
  extension VC: CometChatMessageDelegate {
    func onMessageDeleted(message: BaseMessage) {
      print("Deleted:", message.id, message.deletedAt)
    }
  }
  ```

  **Who can delete:** Message sender, Group admin, Group moderator
  **Deleted fields:** `deletedAt` (timestamp), `deletedBy` (user who deleted)
</Accordion>

Deleting a message is straightforward. Receiving delete events has two parts:

1. Adding a listener for [real-time deletes](#real-time-message-delete-events) when your app is running
2. Fetching [missed deletes](#missed-message-delete-events) when your app was offline

## Delete a Message

Use `CometChat.delete(messageId:)` with the message ID.

```swift theme={null}
CometChat.delete(messageId: 12345, onSuccess: { (message) in
    print("Message deleted: \(message)")
}, onError: { (error) in
    print("Error: \(error.errorDescription)")
})
```

The deleted message object is returned with `deletedAt` (timestamp) and `deletedBy` (UID of deleter) fields set.

The `delete()` method returns a [`BaseMessage`](/sdk/reference/messages#basemessage) object.

<Note>
  This is a soft delete — message content is still available on the object. Check `deletedAt > 0` to identify deleted messages.
</Note>

| User            | 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

The `onMessageDeleted` callback receives a [`BaseMessage`](/sdk/reference/messages#basemessage) object with the `deletedAt` and `deletedBy` fields set.

```swift theme={null}
extension YourViewController: CometChatMessageDelegate {

    func onMessageDeleted(message: BaseMessage) {
        print("Message deleted: \(message.id)")
        print("Deleted at: \(message.deletedAt)")
        print("Deleted by: \(message.deletedBy ?? "")")
    }
}

// Register the delegate:
CometChat.messagedelegate = self
```

## Missed Message Delete Events

When fetching message history, deleted messages have `deletedAt` and `deletedBy` fields set. Additionally, an [`Action`](/sdk/reference/messages#action) message is added to history indicating the deletion.

The [`Action`](/sdk/reference/messages#action) object contains:

* `action` — `deleted`
* `actionOn` — The deleted message object
* `actionBy` — User who deleted the message
* `actionFor` — Receiver (User or Group)

```swift theme={null}
for message in messages {
    if message.deletedAt > 0 {
        print("Message \(message.id) was deleted at \(message.deletedAt)")
    }

    if let actionMessage = message as? ActionMessage {
        if actionMessage.action == .messageDeleted {
            print("Delete action detected")
        }
    }
}
```

<Note>
  You must be the message sender or a group admin/moderator to delete a message.
</Note>

***

## Next Steps

<CardGroup cols={2}>
  <Card title="Edit a Message" icon="pen-to-square" href="/sdk/ios/edit-message">
    Edit sent messages in conversations
  </Card>

  <Card title="Send Messages" icon="paper-plane" href="/sdk/ios/send-message">
    Send text, media, and custom messages
  </Card>

  <Card title="Receive Messages" icon="inbox" href="/sdk/ios/receive-message">
    Listen for incoming messages in real-time
  </Card>

  <Card title="Flag a Message" icon="flag" href="/sdk/ios/flag-message">
    Report inappropriate messages
  </Card>
</CardGroup>
