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 and pass in the AppID
ZIMAppConfig appConfig = ZIMAppConfig();
appConfig.appID = appID;
appConfig.appSign = appSign;
ZIM.create(appConfig);

// 2. Login
try{
    ZIMLoginConfig loginConfig = ZIMLoginConfig();
    // The user nickname, leave it blank if you don't want to modify the user nickname
    loginConfig.userName = 'userName';
    // If you use token as the login authentication method, please fill in this parameter, otherwise no need to fill in
    loginConfig.token = '';
    // Whether this login is an offline login, please refer to the offline login related documentation for details
    loginConfig.isOfflineLogin = false;
    await ZIM.getInstance()?.login('zego', loginConfig);
    // Login successful, write the business logic for successful login
} on PlatformException catch(onError){
    // Login failed
    // Error code for login failure, please refer to the error code table in the integration documentation for handling
    onError.code;
    // Error message for login failure
    onError.message;
}


// 3. Delete specific messages in a conversation
String conversationID = 'xxxx';
List<ZIMMessage> messageList = [];
ZIMMessageDeleteConfig config = ZIMMessageDeleteConfig();
config.isAlsoDeleteServerMessage = false;
await ZIM
    .getInstance()
    !.deleteMessages(
        messageList, conversationID, ZIMConversationType.peer, config)
    .then((value) => {})
    .catchError((onError) {});
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 ZIMMessageDeletedResult.
SampleCode
// 1. Create a ZIM object and pass in the AppID
ZIMAppConfig appConfig = ZIMAppConfig();
appConfig.appID = appID;
appConfig.appSign = appSign;
ZIM.create(appConfig).catchError((onError) {});

// 2. Login
try{
    ZIMLoginConfig loginConfig = ZIMLoginConfig();
    // The user nickname, leave it blank if you don't want to modify the user nickname
    loginConfig.userName = 'userName';
    // If using token for login authentication, please fill in this parameter, otherwise no need to fill in
    loginConfig.token = '';
    // Whether this login is an offline login, please refer to the offline login documentation for details
    loginConfig.isOfflineLogin = true;
    await ZIM.getInstance()?.login('zego', loginConfig);
    // Login successful, write the business logic for successful login
  } on PlatformException catch(onError){
    // Login failed
    // Error code for login failure, please refer to the error code table in the integration documentation for handling
    onError.code;
    // Error message for login failure
    onError.message;
  }


// 3. Delete all messages in the specified conversation
String conversationID = "xxxx";

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

await ZIM
    .getInstance()
    !.deleteAllMessage(conversationID, ZIMConversationType.peer, config)
    .then((value) => {})
    .catchError((onError) {});
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.

After clearing all messages in all conversations:

Untitled
// Delete all messages in conversations

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

await ZIM
    .getInstance()
    !.deleteAllConversationMessages(config)
    .then((value) => {})
    .catchError((onError) {});
1
Copied!

Previous

Get message history

Next

Insert local messages