Skip to main content

Delete Messages

For one-to-one chats, group chats, and system conversations, you can use the IMLib SDK to delete your historical messages. This supports deleting messages only from the local database, only from the RC server, or simultaneously from both the local database and RC server.

The delete message operations in IMLib SDK (APIs listed in the table below) all refer to deleting one or a group of messages from the historical records of the currently logged-in user, without affecting the historical records of other users in the conversation.

FeatureLocal/ServerAPI
[Delete specific messages locally (by Message ID)]Local only (overloaded method)deleteMessages
[Delete all local conversation history]Local only (overloaded method)deleteMessages
[Delete specific messages in conversation (by Message object)]Delete from both local and serverdeleteRemoteMessages
[Delete conversation history (by timestamp)]Optional: local only or both local and servercleanHistoryMessages
[Delete conversation history from server only (by timestamp)]Server onlycleanRemoteHistoryMessages
tip
  • By default, messages in one-to-one chats, group chats, and system conversations for App users are stored in both the local database and on the RC server (default 6 months). The Cloud Storage for One-to-One and Group Messages service is enabled by default, so messages can be deleted from both local and remote historical records.

  • For one-to-one chats and group chats, if any interface 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 when the Multi-Device Message Synchronization switch is turned on). In this case, if you log in on another device or reinstall the app, you may still receive deleted historical messages due to the message compensation mechanism. To completely delete message compensation, [submit a ticket] to apply for enabling Delete offline message compensation when deleting server-side historical messages. If remote messages are deleted by passing message objects, the server will definitely 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, the recall message feature should be used. After a message is successfully recalled, the original message content will be deleted from the local and server historical records of all users.

  • IMLib SDK does not provide an interface to delete chatroom messages. The current user's local chatroom messages are automatically deleted when exiting the chatroom. After enabling the Chatroom Message Cloud Storage Service, if you need to clear chatroom historical messages for all users, you can use the server API Delete Messages.

  • IMLib SDK provides APIs to delete messages in ultra group conversations. For details, see Delete Messages under "Ultra Group Management".

Sample Code

ConversationType conversationType = ConversationType.PRIVATE;
String targetId = "conversation ID";
Message[] messages = {message1, message2};

RongCoreClient.getInstance().deleteRemoteMessages(conversationType, targetId, messages, new IRongCoreCallback.OperationCallback() {
/**
* Callback for successful message deletion
*/
@Override
public void onSuccess() {

}
/**
* Callback for failed message deletion
* @param errorCode Error code
*/
@Override
public void onError(IRongCoreEnum.CoreErrorCode coreErrorCode) {

}
});

Delete Conversation History Messages (Timestamp)

tip

This method can clear both server-side and local history messages. The Cloud Storage for One-to-One and Group Messages service is enabled by default, so you can clear server-side messages directly.

If app users want to delete a segment of history messages from their one-to-one chats, group chats, or system conversations, they can use cleanHistoryMessages to delete messages older than a specified timestamp (recordTime). The cleanRemote parameter controls whether to simultaneously delete corresponding history messages on the server.

When cleanRemote is set to true, messages older than recordTime will be deleted from both local and server storage. After server-side deletion, the user can no longer retrieve these messages from the server. This interface only allows deleting messages from a single specified conversation at a time, identified by conversation type and conversation ID parameters.

Interface

RongCoreClient.getInstance().cleanHistoryMessages(conversationType, targetId, recordTime, cleanRemote,callback);

Parameter Description

ParameterTypeDescription
conversationTypeConversationTypeConversation type
targetIdStringConversation ID
recordTimelongTimestamp. By default, deletes all messages with timestamps less than or equal to recordTime. Passing 0 will delete all messages.
cleanRemotebooleanWhether to delete server-side messages. true: Delete corresponding server-side messages. false: Do not delete server-side messages.
callbackOperationCallbackInterface callback

Sample Code

ConversationType conversationType = ConversationType.PRIVATE;
String targetId = "conversation ID";
String recordTime = 0;
Boolean cleanRemote = true; // Also delete corresponding message history from the server

RongCoreClient.getInstance().cleanHistoryMessages(conversationType, targetId, recordTime, cleanRemote,
new IRongCoreCallback.OperationCallback() {
/**
* Callback for successful message deletion
*/
@Override
public void onSuccess() {

}
/**
* Callback for failed message deletion
* @param errorCode Error code
*/
@Override
public void onError(IRongCoreEnum.CoreErrorCode coreErrorCode) {

}
});

Delete Only Server-Side Conversation History Messages (Timestamp)

tip
  • The Cloud Storage for One-to-One and Group Messages service is enabled by default. You can call the following interface to delete history message records stored on RC servers. This interface only deletes messages from the server.
  • You can also directly delete server-side messages using RC's IM Server API. For details, refer to the server documentation Message Deletion.

App users can delete only the history messages stored on the server for specified conversations. This interface provides a timestamp parameter (recordTime) to delete messages older than the specified time. Setting recordTime to 0 will delete all history messages for the conversation stored on the server.

Interface

RongCoreClient.getInstance().cleanRemoteHistoryMessages(conversationType, targetId, recordTime,callback);

Parameter Description

ParameterTypeDescription
conversationTypeConversationTypeConversation type
targetIdStringConversation ID
recordTimelongTimestamp. By default, deletes all messages with send timestamps less than or equal to this value. Passing 0 clears all messages in the conversation.
callbackOperationCallbackInterface callback

Sample Code

ConversationType conversationType = ConversationType.PRIVATE
String targetId = "conversation ID";
String recordTime = 0;

RongCoreClient.getInstance().cleanRemoteHistoryMessages(conversationType, targetId, recordTime, new IRongCoreCallback.OperationCallback() {
/**
* Callback for successful message deletion
*/
@Override
public void onSuccess() {

}
/**
* Callback for failed message deletion
* @param errorCode Error code
*/
@Override
public void onError(IRongCoreEnum.CoreErrorCode coreErrorCode) {

}
});