In-app Chat
  • iOS : Objective-C
  • Android
  • Web
  • Flutter
  • React Native
  • Unity3D
  • Windows
  • macOS
  • Introduction
    • Overview
    • Basic concepts
  • Sample app
  • Getting started
  • Client SDKs
    • SDK downloads
    • Release notes
    • Upgrade guide
  • Guides
    • Authentication
    • Manage users
    • Room
    • Group
    • Messaging
    • Call invitation (signaling)
    • Manage sessions
  • Offline push notifications
  • Error codes
  • Client APIs
  • Server APIs
  • Documentation
  • In-app Chat
  • Guides
  • Messaging
  • Delete messages

Delete messages

Last updated:2024-04-30 17:42

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.

Prerequisites

  • A project is created in the ZEGOCLOUD console, and the application ID and the application signature for connecting to ZIM SDK are obtained. The ZIM service is activated in the ZEGOCLOUD console. For more information, see Authentication. If the ZIM service cannot be activated, contact ZEGOCLOUD technical support for help.
  • Your project is integrated with ZIM SDK. For more information, see the "Integrate the SDK" section of the Send and receive messages topic.

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 deleteMessages method and pass the messageList and config parameters.
    3. Client A receives the results through ZIMMessageDeletedCallback.

Sample method call

//An array of messages to delete.
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.  
}];

Parameter description

Parameter Type Required Description
messageList ArrayList Yes The list of specified messages to delete.
conversationID String Yes

Session ID.

  • In one-on-one chat, conversationID is the peer side's userID.
  • In group chat, conversationID is the groupID.
conversationType ZIMConversationType Yes

Session type.

  • 0: One-on-one chat.
  • 2: Group chat.
config ZIMMessageDeleteConfig Yes Advanced configurations of the message you want to delete.
callback ZIMMessageDeletedCallback Yes Callback for the results of delete the specified messages of the session.

You will need to configure the config parameter of the class ZIMMessageDeleteConfig according to the following:

Parameter Type Description
isAlsoDeleteServerMessage
Boolean
Whether to delete messages from the server.

  • true: delete.
  • false: do not delete.

Sample code

// 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. 
}];

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.

Sample method call

// Delete all messages of the specified session.
ZIMMessageDeleteConfig *config = [[ZIMMessageDeleteConfig alloc] init];
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.  
}];

Parameter description

Parameter Type Required Description
conversationID string Yes

Session ID.

  • In one-on-one chat, conversationID is the peer side's userID.
  • In group chat, conversationID is the groupID.
conversationype ZIMConversationType Yes

Session type.

  • 0: One-on-one chat.
  • 2: Group chat.
config ZIMMessageDeleteConfig Yes Advanced configurations of the message you want to delete.
callback ZIMMessageDeletedCallback Yes Callback for the results of delete all messages of the specified session.

You will need to configure the config parameter of the class ZIMMessageDeleteConfig according to the following:

Parameter Type Description
isAlsoDeleteServerMessage
Boolean
Whether to delete messages from the server.

  • true: delete
  • false: do not delete

Sample code

// 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.   
    }];

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:

// 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.   
    }];
Page Directory