Skip to main content

Delete Message

The SDK supports directly deleting historical messages of one-to-one chats and group chats stored on the RC server from the client. You can delete a specified single message or a group of messages, or delete messages in a conversation that are older than a specified timestamp.

tip
  • The Cloud Storage for One-to-One and Group Messages service is enabled by default. User messages are stored on the RC server (default retention period: 6 months).
  • For one-to-one chats and group chats, if any API is used to delete remote messages by passing a timestamp, the server will not delete the corresponding offline message compensation by default (this mechanism only takes effect after enabling the Multi-Device Message Synchronization switch). In this case, if you log in on another device or reinstall the app, you may still receive the deleted historical messages due to the message compensation mechanism. To completely delete message compensation, [submit a ticket] to request enabling deletion of offline message compensation across all devices when deleting historical messages from the server. If remote messages are deleted by passing message objects, the server will always delete the corresponding messages in the message compensation.
  • For one-to-one chats and group chats, if an app administrator or a regular user wants to completely delete a message from the historical records of all conversation participants, they should use the message recall feature. After a message is successfully recalled, the original message content will be deleted from the local and server historical message records of all users.
  • The client SDK does not provide an interface for deleting chatroom messages. Local chatroom messages of the current user will be automatically deleted when exiting the chatroom. After enabling the Cloud Storage for Chatroom Messages service, if you need to clear chatroom historical messages for all users, you can use the server API Delete Messages.

Delete by Message

Call [deleteMessages] to delete one or a group of specified messages from a single conversation on the server based on message IDs, timestamps, and direction (sent or received).

Interface

RongIMLib.deleteMessages(conversation, messages);


#### Parameter Description

| Parameter | Type | Required | Description |
|:--- |:--- |:--- |:--- |
|conversation|[IConversationOption]| Yes | Target conversation |
|messages| Message List | Yes | List of messages to delete. See structure details below. |


- **`messages` Structure Details**

| Parameter | Type | Required | Description |
|:--- |:--- |:--- |:--- |
|messageUId| string | Yes | Message UId|
|sentTime|number | Yes |Message sending time|
|messageDirection | Yes | [MessageDirection]|Message direction|


#### Example Code

```js
const conversation = {
conversationType: RongIMLib.ConversationType.PRIVATE,
targetId: "<Target User ID>"
}
RongIMLib.deleteMessages(conversation, [
{
messageUId: "BS4O-P5AO-D1O6-9GPP",
sentTime: 1632728405345,
messageDirection: RongIMLib.MessageDirection.SEND
},
{
messageUId: "BS4O-QEBR-VJM6-9GPP",
sentTime: 1632728573423,
messageDirection: RongIMLib.MessageDirection.SEND
}
]).then(res => {
if (res.code === 0) {
console.log('Deletion successful')
} else {
console.log(res.code, res.msg)
}
}).catch(error => {
console.log(error)
})


## Delete by Timestamp

Call the [clearHistoryMessages] method to delete historical messages in a specified conversation that are older than a certain timestamp.


#### Interface

```js
RongIMLib.clearHistoryMessages(conversation, timestamp)


#### Parameter Description

| Parameter | Type | Required | Description |
|:--- |:--- |:--- |:--- |
| conversation | [IConversationOption] | Yes | Target conversation |
| timestamp | number | Yes | Timestamp |


#### Example Code

```js
const conversation = {
conversationType: RongIMLib.ConversationType.PRIVATE,
targetId: "<Target User ID>"
}
RongIMLib.clearHistoryMessages(conversation, timestamp).then(res => {
if (res.code === 0) {
console.log('Cleared successfully')
} else {
console.log(res.code, res.msg)
}
}).catch(error => {
console.log(error)
})

<!-- links-->
<!-- API reference -->
[Submit a ticket]: https://console.rongcloud.io/agile/formwork/ticket/create
[IConversationOption]: https://doc.rongcloud.cn/apidoc/im-web/latest/zh_CN/modules.html#IConversationOption
[MessageDirection]: https://doc.rongcloud.cn/apidoc/im-web/latest/zh_CN/enums/MessageDirection.html
[deleteMessages]: https://doc.rongcloud.cn/apidoc/im-web/latest/zh_CN/modules.html#deleteMessages
[clearHistoryMessages]: https://doc.rongcloud.cn/apidoc/im-web/latest/zh_CN/modules.html#clearHistoryMessages