Skip to main content

Delete Messages

RC supports app users in deleting their historical messages in one-to-one chat, group chat, and system conversation via the client SDK. Users can choose to delete messages from the local database only, from the RC server only, or from both locations simultaneously.

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

FeatureLocal/ServerAPI
Delete specified messages by message ID (local only)Delete from local only (overloaded method)deleteMessages
Delete all historical messages of a conversation (local only)Delete from local only (overloaded method)deleteMessages
Delete specified messages in a conversation (message object)Delete from both local and serverdeleteRemoteMessages
Delete historical messages of a conversation (timestamp)Option to delete from local only or both local and servercleanHistoryMessages
Delete historical messages of a conversation from server only (timestamp)Delete from server onlycleanRemoteHistoryMessages
tip
  • By default, messages in one-to-one chat, group chat, and system conversation are stored only in the local database, and can only be deleted locally. If the app (App Key/environment) has enabled Cloud Storage for One-to-One and Group Messages, the user's messages will also be stored on the RC server (default 6 months), and can be deleted from the remote historical message records.
  • For one-to-one chat and group chat, 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 after the Multi-Device Message Synchronization switch is turned on). 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 the message compensation, please submit a ticket to apply for enabling Delete offline message compensation for multi-device synchronization when deleting historical messages from the server. If remote messages are deleted by passing a message object, the server will definitely delete the corresponding messages in the message compensation.
  • For one-to-one chat and group chat, if an app administrator or a regular user wants to completely delete a message from the historical records of all participants in the conversation, the recall message function should be used. After the 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 to delete chatroom messages. The local chatroom messages of the current user will be automatically deleted when they leave the chatroom. If the Cloud Storage for Chatroom Messages service is enabled, and you need to clear the historical messages of all users in the chatroom, you can use the server API Delete Messages.

Delete Specified Messages by Message ID (Local Only)

App users can delete messages stored in the local database by message ID. The messages to be deleted can belong to different conversations. A maximum of 100 messages can be deleted at a time.

If the app has enabled the Cloud Storage for One-to-One and Group Messages service, the historical message records of the user stored on the server will not be affected. If the user retrieves historical messages from the server, they may receive messages that have been deleted locally.

int[] messageIds = {12, 13};
RongCoreClient.getInstance().deleteMessages(messageIds, new IRongCoreCallback.ResultCallback<Boolean>() {
/**
* Callback for successful message deletion
*/
@Override
public void onSuccess(Boolean bool) {

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

}
});

An array of message IDs to be deleted is required.

ParameterTypeDescription
messageIdsint[]Array of message IDs. See the MessageId property in Message Introduction.
callbackIRongCoreCallback.ResultCallback<Boolean>Interface callback

Delete All Historical Messages of a Conversation (Local Only)

App users may want to clear their historical records of one-to-one chat, group chat, or system conversation on the device locally. They can delete all messages of a specified conversation stored in the local database.

If the app has enabled the Cloud Storage for One-to-One and Group Messages service, the historical message records of the user stored on the server will not be affected. If the user retrieves historical messages from the server, they may receive messages that have been deleted locally.

ConversationType conversationType = ConversationType.PRIVATE;
String targetId = "Conversation Id";

RongCoreClient.getInstance().deleteMessages(conversationType, targetId, new IRongCoreCallback.ResultCallback<Boolean>() {
/**
* Callback for successful message deletion
*/
@Override
public void onSuccess(Boolean bool) {

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

}
});

This interface allows deleting messages of only one specified conversation at a time. The conversation is specified by the conversation type and conversation ID parameters.

ParameterTypeDescription
conversationTypeConversationTypeConversation type
targetIdStringConversation ID
callbackIRongCoreCallback.ResultCallback<Boolean>Interface callback

Delete Specified Messages in a Conversation (Message Object)

tip

If the app has enabled the Cloud Storage for One-to-One and Group Messages service, you can call the following interface to delete the historical message records of the app user on the RC server. This interface deletes messages from both local and server.

If an app user wants to completely delete a message from their historical records of one-to-one chat, group chat, or system conversation, they can use deleteRemoteMessages to delete the message from both local and server. After successful deletion, the user cannot retrieve the message from the local database. If they retrieve historical messages from the server, they will not be able to retrieve the deleted message either.

This interface allows deleting one or a group of messages in a specified conversation at a time. Please ensure that the provided messages all belong to the same conversation (specified by the conversation type and conversation ID). A maximum of 100 messages can be deleted at a time.

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) {

}
});
ParameterTypeDescription
conversationTypeConversationTypeConversation type. Chatroom is not supported.
targetIdStringConversation ID
messagesMessage[]Array of message objects Message to be deleted. Ensure that the provided messages all belong to the same conversation.
callbackIRongCoreCallback.ResultCallback<Boolean>Interface callback

Delete Historical Messages of a Conversation (Timestamp)

tip

If the app has enabled the Cloud Storage for One-to-One and Group Messages service, you can call the following interface and specify the parameters to delete the historical message records of the app user on the RC server simultaneously.

If an app user wants to delete a segment of historical message records from their one-to-one chat, group chat, or system conversation, they can use cleanHistoryMessages to delete messages in the conversation that are earlier than a certain point in time (recordTime). The cleanRemote parameter controls whether to delete the corresponding historical messages from the server simultaneously.

ConversationType conversationType = ConversationType.PRIVATE;
String targetId = "Conversation Id";
String recordTime = 0;
Boolean cleanRemote = true; // Also delete the 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) {

}
});

If the cleanRemote parameter is set to true, it means that the corresponding messages on the server need to be deleted simultaneously. This interface will delete messages earlier than (recordTime) from both local and server. After the server messages are deleted, the user will not be able to retrieve the deleted messages from the server. This interface allows deleting messages of only one specified conversation at a time. The conversation is specified by the conversation type and conversation ID parameters.

ParameterTypeDescription
conversationTypeConversationTypeConversation type
targetIdStringConversation ID
recordTimelongTimestamp. By default, messages with a send time less than or equal to recordTime are deleted. If 0 is passed, all messages are deleted.
cleanRemotebooleanWhether to delete messages from the server. true: Also delete corresponding messages from the server. false: Do not delete corresponding messages from the server.
callbackOperationCallbackInterface callback

Delete Historical Messages of a Conversation from Server Only (Timestamp)

tip
  • If the app has enabled the Cloud Storage for One-to-One and Group Messages service, you can call the following interface to delete the historical message records of the app user on the RC server. This interface deletes messages from the server only.
  • You can also directly delete server messages using the RC IM server API. For specific operations, please refer to the server documentation Delete Messages.

App users can delete historical messages of a specified conversation stored on the server only. This interface provides a timestamp parameter (recordTime) to support deleting messages earlier than the specified time. If recordTime is set to 0, all historical messages of the conversation stored on the server will be deleted.

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) {

}
});
ParameterTypeDescription
conversationTypeConversationTypeConversation type
targetIdStringConversation ID
recordTimelongTimestamp. By default, all messages with a send time less than or equal to this timestamp are deleted. Passing 0 means clearing all messages in the conversation.
callbackOperationCallbackInterface callback