Delete Messages (Electron)
This document is only applicable to the Electron Solution and should be used exclusively with the Electron modules ([@rongcloud/electron] and [@rongcloud/electron-renderer]).
-
By default, messages in one-to-one chats, group chats, and system conversations for app users are stored in the local database and can be deleted locally. 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) and can be deleted from remote historical message records.
-
For one-to-one and group chats, if remote messages are deleted by passing a timestamp through any API, the server will not delete the corresponding offline message compensation by default (this mechanism only takes effect when the Multi-Device Message Synchronization switch is enabled). In this case, if the user logs in on another device or reinstalls the app, they may still receive the deleted historical messages due to the message compensation mechanism. To completely delete message compensation, please [submit a ticket] to request enabling deletion of offline message compensation across devices when deleting historical messages from the server. If remote messages are deleted by passing the message object, the server will always delete the corresponding messages in the message compensation.
-
For one-to-one and group chats, if an app administrator or regular user wants to permanently delete a message from the historical records of all conversation participants, they should use the message recall feature. Once a message is successfully recalled, the original message content will be deleted from both the local and server historical records of all users.
-
The client SDK does not provide an interface for deleting chatroom messages. Local chatroom messages for the current user are automatically deleted when they exit 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.
This document explains how to delete local client messages via API.
Delete Messages by Conversation
This interface is supported starting from SDK 5.4.0.
Call the [electronExtension.clearMessages] interface to delete all messages of a specified conversation from the local message database.
Interface
RongIMLib.electronExtension.clearMessages(conversation)
#### Parameters
| Parameter | Type | Required | Description |
|:--- |:--- |:--- |:--- |
| conversation | [IConversationOption] | Yes | Target conversation |
#### Example Code
```js
const conversation = {
conversationType: RongIMLib.ConversationType.PRIVATE,
targetId: "<Target User ID>"
}
RongIMLib.electronExtension.clearMessages(conversation).then(res => {
if (res.code === 0) {
console.log('Successfully cleared messages in this conversation');
} else {
console.log(res.code, res.msg);
}
}).catch(error => {
console.log(error)
})
## Delete Messages by Timestamp \{#deleteMessagesByTimestamp}
:::tip
This interface is supported starting from SDK 5.4.0.
:::
Call the [electronExtension.deleteMessagesByTimestamp] interface to delete local message records in a specified conversation that are older than the given timestamp.
#### Interface
```js
RongIMLib.electronExtension.deleteMessagesByTimestamp(conversation, timestamp, cleanSpace)
#### Parameters
| Parameter | Type | Required | Description |
|:--- |:--- |:--- |:--- |
| conversation | [IConversationOption] | Yes | Target conversation |
| timestamp | number | Yes | Timestamp |
| cleanSpace | boolean | No | Whether to clean up the disk space used by the data entries |
:::warning
Cleaning up disk space will block the process and take a long time, so it is not recommended. If data is erased, the unused disk space will be reused in subsequent storage operations and will not affect data queries.
:::
#### Example Code
```js
const conversation = {
conversationType: RongIMLib.ConversationType.PRIVATE,
targetId: "<Target User ID>"
}
const timestamp = 1632728573423
RongIMLib.electronExtension.deleteMessagesByTimestamp(conversation, timestamp, false).then(res => {
if (res.code === 0) {
console.log('Successfully cleared messages in this conversation');
} else {
console.log(res.code, res.msg);
}
}).catch(error => {
console.log(error)
})
## Delete Messages by Message ID \{#deleteMessages}
:::tip
This interface is supported starting from SDK 5.4.0.
:::
Call the [electronExtension.deleteMessages] interface to delete one or more messages from the local message database by message ID. **Note: If deleting multiple messages, ensure all message IDs belong to the same conversation.**
#### Interface
```js
RongIMLib.electronExtension.deleteMessages(messageIds)
#### Parameters
| Parameter | Type | Required | Description |
|:--- |:--- |:--- |:--- |
| messageIds | string[] | Yes | Array of message IDs, which must belong to the same conversation |
#### Example Code
```js
const messageIds = [<messageId>]
RongIMLib.electronExtension.deleteMessages(messageIds).then(res => {
if (res.code === 0) {
console.log('Successfully deleted messages');
} else {
console.log(res.code);
}
}).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
[electronExtension.clearMessages]: https://doc.rongcloud.cn/apidoc/im-web/latest/zh_CN/modules/electronExtension.html#clearMessages
[electronExtension.deleteMessagesByTimestamp]: https://doc.rongcloud.cn/apidoc/im-web/latest/zh_CN/modules/electronExtension.html#deleteMessagesByTimestamp
[electronExtension.deleteMessages]: https://doc.rongcloud.cn/apidoc/im-web/latest/zh_CN/modules/electronExtension.html#deleteMessages
[@rongcloud/electron]: https://www.npmjs.com/package/@rongcloud/electron
[@rongcloud/electron-renderer]: https://www.npmjs.com/package/@rongcloud/electron-renderer