logo
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 and appSign
ZIMAppConfig *appConfig = [[ZIMAppConfig alloc] init];
 appConfig.appID = (unsigned int)appID;     ///Replace with the AppID you applied for
 appConfig.appSign = @"appSign";     //Replace with the AppSign you applied for
 self.zim = [ZIM createWithAppConfig: appConfig];

// 2. Log in
ZIMUserInfo *userInfo = [[ZIMUserInfo alloc] init];
userInfo.userID = @"xxxx";
userInfo.userName = @"xxxx";
[self.zim loginWithUserInfo:userInfo callback:^(ZIMError * _Nonnull errorInfo){
    //Developers can use ZIMError to determine whether the login is successful.
}];

// 3. Delete the specified messages.
NSMutableArray *deleteMessageList = [[NSMutableArray alloc] init];
ZIMMessageDeleteConfig *config = [[ZIMMessageDeleteConfig alloc] init];
//Used to determine whether to delete messages from the server.
config.isAlsoDeleteServerMessage = true;
[self.zim deleteMessages:messageList conversationID:conversationID conversationType:conversationType config:config callback:^(NSString * _Nonnull conversationID, ZIMConversationType conversationType, ZIMError * _Nonnull errorInfo) {
    // You can listen for the callback to check whether the messages are deleted successfully. 
}];
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 deleteAllMessageByConversationID method and pass the conversationID, conversationType, and config parameters.
    3. Client A receives the results through the callback ZIMMessageDeletedCallback.
SampleCode
// 1. Create a ZIM object, pass in appID and appSign
ZIMAppConfig *appConfig = [[ZIMAppConfig alloc] init];
 appConfig.appID = (unsigned int)appID;     ///Replace with the AppID you applied for
 appConfig.appSign = @"appSign";     //Replace with the AppSign you applied for
 self.zim = [ZIM createWithAppConfig: appConfig];
// 2. Log in
ZIMUserInfo *userInfo = [[ZIMUserInfo alloc] init];
userInfo.userID = @"xxxx";
userInfo.userName = @"xxxx";
[self.zim loginWithUserInfo:userInfo callback:^(ZIMError * _Nonnull errorInfo){
    //Developers can use ZIMError to determine whether the login is successful.
}];

// 3. Delete all messages of the specified session.
NSMutableArray *deleteMessageList = [[NSMutableArray alloc] init];
ZIMMessageDeleteConfig *config = [[ZIMMessageDeleteConfig alloc] init];
//Used to determine whether to delete messages from the server.
config.isAlsoDeleteServerMessage = true;

[self.zim deleteAllMessageByConversationID:conversationID conversationType:conversationType config:config callback:^(NSString * _Nonnull conversationID, ZIMConversationType conversationType, ZIMError * _Nonnull errorInfo) {
     // You can listen for the callback to check whether the messages are deleted successfully.   
    }];
1
Copied!

Delete all messages

After logging into the ZIM SDK, you can call the deleteAllConversationMessagesWithConfig 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 messageDeleted.

After clearing all messages in all conversations:

Untitled
// Delete all messages in all conversations

// et whether to delete server messages
ZIMMessageDeleteConfig *config = [[ZIMMessageDeleteConfig alloc] init];
// Whether to delete server messages
config.isAlsoDeleteServerMessage = true;

[self.zim deleteAllConversationMessagesWithConfig:config callback:^(ZIMError * _Nonnull errorInfo) {
     // Developers can use this callback to listen for successful message deletion.   
    }];
1
Copied!

Previous

Get message history

Next

Insert local messages