logo
In-app Chat
SDK Error Codes
Powered Byspreading
On this page

Delete messages


Overview

ZEGOCLOUD's In-app Chat (the ZIM SDK) provides the capability of message management, allowing you to send and receive one-to-one, group, in-room messages, query message history, delete messages, and more. With the message management feature, you can meet different requirements of various scenarios such as social entertainment, online shopping, online education, interactive live streaming, and more.

This document describes how to delete the specified messages in a specified session, or delete all the messages in a specified session.

Implementation process

The ZIM SDK supports deleting specific messages in a conversation or deleting all messages in a conversation. Deleting messages can be divided into "delete local message records" and "delete server message records". Developers can use the ZIMMessageDeleteConfig object to set advanced properties for deleting messages.

Taking the example of client A deleting certain messages or all messages with client B:

Delete the specified messages

The following process shows how Client A deletes the specified messages with Client B:

  1. Client A and Client B log in to the ZIM SDK to send and receive messages to and from each other.
  2. When Client A wants to delete the specified messages with Client B:
    1. Client A logs in to the ZIM SDK first.
    2. Client A calls the deleteMessagesmethod and pass the messageList and config parameters.
    3. Client A receives the results through the callback ZIMMessageDeleteConfig.
SampleCode
//1. Create a ZIM object, pass in appID, appSign and Application in Android
ZIMAppConfig appConfig = new ZIMAppConfig();
appConfig.appID = 12345;  //Replace with the AppID you applied for
appConfig.appSign = "appSign";   //Replace with the AppSign you applied for
im = ZIM.create(appConfig, application);
// 2. Log in
ZIMUserInfo zimUserInfo = new ZIMUserInfo();
zimUserInfo.userID = "xxxx";
zimUserInfo.userName = "xxxx";
zim.login(zimUserInfo, new ZIMLoggedInCallback() {
    @Override
    public void onLoggedIn(ZIMError error) {
    
    }
});

// 3. Delete the specified messages.
String conversationID = "xxxx";
ArrayList<ZIMMessage> deleteMessageList = new ArrayList();

ZIMMessageDeleteConfig config = new ZIMMessageDeleteConfig();
config.isAlsoDeleteServerMessage = false;

zim.deleteMessages(deleteMessageList, conversationID, ZIMConversationType.Peer, config, new ZIMMessageDeletedCallback() {
    @Override
    public void onMessageDeleted(ZIMError error) {

    }
});
1
Copied!

Delete all messages of the specified session

The following process shows how Client A deletes all messages with Client B:

  1. Client A and Client B log in to the ZIM SDK to send and receive messages to and from each other.
  2. When Client A wants to delete all messages with Client B:
    1. Client A logs in to the ZIM SDK first.
    2. Client A calls the deleteAllMessage method and pass the conversationID, conversationType, and config parameters.
    3. Client A receives the results through the callback ZIMMessageDeletedCallback.
Untitled
// 1. Create a ZIM object, pass in appID, appSign and Application in Android
ZIMAppConfig appConfig = new ZIMAppConfig();
appConfig.appID = 12345;  //Replace with the AppID you applied for
appConfig.appSign = "appSign";   //Replace with the AppSign you applied for
zim = ZIM.create(appConfig, application);

// 2. Log in
ZIMUserInfo zimUserInfo = new ZIMUserInfo();
zimUserInfo.userID = "xxxx";
zimUserInfo.userName = "xxxx";
zim.login(zimUserInfo, new ZIMLoggedInCallback() {
    @Override
    public void onLoggedIn(ZIMError error) {
       
    }
 });

// 3. Delete all messages of the specified session.
String conversationID = "xxxx";

ZIMMessageDeleteConfig config = new ZIMMessageDeleteConfig();
config.isAlsoDeleteServerMessage = false;

zim.deleteAllMessage(conversationID, ZIMConversationType.Peer, config, new ZIMMessageDeletedCallback() {
    @Override
    public void onMessageDeleted(ZIMError error) {

    }
});
1
Copied!

Delete all messages

After logging into the ZIM SDK, you can call the deleteAllConversationMessages method and pass the ZIMMessageDeleteConfig parameter to configure whether to delete messages stored on the server. This will delete all messages in one-on-one and group conversations.

The result of the deletion operation will be returned through the ZIMConversationMessagesAllDeletedCallback callback interface. Additionally, the client will also receive a notification for onMessageDeleted.

After clearing all messages in all conversations:

Untitled
//Delete all messages in all conversations

// Set whether to delete messages stored on the server
ZIMMessageDeleteConfig config = new ZIMMessageDeleteConfig();
config.isAlsoDeleteServerMessage = true;

zim.deleteAllConversationMessages(config, new ZIMConversationMessagesAllDeletedCallback() {
    @Override
    public void onConversationMessagesAllDeleted(ZIMError error) {
        // Developers can use this callback to monitor whether the message is deleted successfully.
    }
});

1
Copied!

Previous

Get message history

Next

Insert local messages