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:
- Client A and Client B log in to the ZIM SDK to send and receive messages to and from each other.
- When Client A wants to delete the specified messages with Client B:
- Client A logs in to the ZIM SDK first.
- Client A calls the deleteMessagesmethod and pass the
messageList
and config
parameters.
- Client A receives the results through the callback ZIMMessageDeleteConfig.
//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
// 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
// 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
// 1. Create a ZIM object, pass in appID and appSign. Currently, it is recommended to create only one zim instance per client
zim::ZIMAppConfig app_config;
app_config.appID = 12345; //Replace with the AppID you applied for
app_config.appSign = "appSign"; //Replace with the AppSign you applied for
zim_ = zim::ZIM::create(app_config);
// 2. Set setEventHandler callback
im_event_handler_ = std::make_shared<CZIMEventHandler>();
zim_->setEventHandler(im_event_handler_);
// 3. Log in
zim::ZIMUserInfo user_info;
user_info.userID = user_id;
user_info.userName = user_name;
zim_->login(user_info, [=](zim::ZIMError errorInfo){
//Here you can get the login result return value and execute the user code according to the error code
});
// 4. Delete the specified messages.
std::vector<std::shared_ptr<ZIMMessage>> messageList;
zim::ZIMMessageDeleteConfig config;
// Used to determine whether to delete messages from the server.
config.isAlsoDeleteServerMessage = true;
zim_->deleteMessages(messageList,"conversationID",zim::ZIMConversationTypePeer,config,callback);
1
// 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
// Delete the specified message of the conversation
var deleteMessageList = [];
var conversationID = '';
var conversationType = 0;
var config = { isAlsoDeleteServerMessage: false };
zim.deleteMessages(deleteMessageList, conversationID, conversationType, config)
.then(function ({ conversationID, conversationType }) {
// Operation successful.
})
.catch(function (err) {
// Operation failed.
});
1
Delete all messages of the specified session
The following process shows how Client A deletes all messages with Client B:
- Client A and Client B log in to the ZIM SDK to send and receive messages to and from each other.
- When Client A wants to delete all messages with Client B:
- Client A logs in to the ZIM SDK first.
- Client A calls the deleteAllMessage method and pass the
conversationID
, conversationType
, and config
parameters.
- Client A receives the results through the callback ZIMMessageDeletedResult.
// 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
// 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
// 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
// 1. Create a ZIM object, pass in appID and appSign. Currently, it is recommended to create only one zim instance per client
zim::ZIMAppConfig app_config;
app_config.appID = 12345; //Replace with the AppID you applied for
app_config.appSign = "appSign"; //Replace with the AppSign you applied for
zim_ = zim::ZIM::create(app_config);
// 2. Set setEventHandler callback
im_event_handler_ = std::make_shared<CZIMEventHandler>();
zim_->setEventHandler(im_event_handler_);
// 3. Log in
zim::ZIMUserInfo user_info;
user_info.userID = user_id;
user_info.userName = user_name;
zim_->login(user_info, [=](zim::ZIMError errorInfo){
//Here you can get the login result return value and execute the user code according to the error code
});
});
//Used to determine whether to delete messages from the server.
ZIMMessageDeleteConfig config;
config.isAlsoDeleteServerMessage = true;
zim_->deleteAllMessage("converastionID",zim::ZIMConversationTypePeer,config,callback);
1
// 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
// Delete all messages of the specified session.
var conversationID = '';
var conversationType = 0;
var config = { isAlsoDeleteServerMessage: true };
zim.deleteAllMessage(conversationID, conversationType, config)
.then(function ({ conversationID, conversationType }) {
// Operation successful.
})
.catch(function (err) {
// Operation failed.
});
1
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 ZIMConversationMessagesAllDeletedResult callback interface. Additionally, the client will also receive a notification for messageDeleted.
The result of the deletion operation will be returned through the ZIMConversationMessagesAllDeletedResult callback interface. Additionally, the client will also receive a notification for messageDeleted.
The result of the deletion operation will be returned through the ZIMConversationMessagesAllDeletedResult callback interface. Additionally, the client will also receive a notification for messageDeleted.
The result of the deletion operation will be returned through the ZIMConversationMessagesAllDeletedResult 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
// 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
// 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
// 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
// Delete all messages in all conversations
// Set whether to delete messages stored on the server
zim::ZIMMessageDeleteConfig config;
config.isAlsoDeleteServerMessage = true;
zim::ZIM::getInstance()->deleteAllConversationMessages(config, [=](const zim::ZIMError& errorInfo) {
// Result of deleting messages
if (errorInfo.code == zim::ZIM_ERROR_CODE_SUCCESS) {
// Business logic after deletion
}
else {
// Please check the error code document for solutions
}
});
1
// 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
// Delete all messages in all conversations
// Set whether to delete messages stored on the server
const config = {
isAlsoDeleteServerMessage: true
}
zim.deleteAllConversationMessages(config)
.then(function ({ conversationID, conversationType }) {
// Operation successful.
})
.catch(function (err) {
// Operation failed.
1