In-app Chat
SDK Error Codes
On this page

Delete messages

2026-09-18

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, delete all the messages in a specified session, and delete the specified messages for everyone in a session.

Warning

The ZIM SDK currently supports deleting messages in "one-to-one" and "group" conversations, but does not support deleting messages in "room" conversations.

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.

By default, a deletion operation only takes effect for the current user. To make the specified messages invisible to all members in the conversation, refer to Delete for Everyone.

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.
// 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 for Everyone

Note

This feature is supported since the ZIM SDK 3.2.0.

By default, a deletion operation only takes effect for the current user, and other members in the conversation can still see the deleted messages. To make the specified messages invisible to all members in the conversation, set isAlsoDeleteForEveryone of ZIMMessageDeleteConfig to true when calling deleteMessages.

After the deletion succeeds, the operator and other members in the conversation will receive the deletion result notifications respectively:

  • The operator learns the deletion result through the ZIMMessageDeletedCallback callback.
  • Other members in the conversation receive the notification through the messageDeleted callback. In the callback parameter ZIMMessageDeletedInfo, messageDeleteType is the "delete specified messages" type, and messageList is the list of deleted messages.

The following sample code shows how to delete messages for everyone, and how the operator and other members receive the deletion result notifications:

// Delete the specified messages for everyone in the conversation
NSMutableArray *deleteMessageList = [[NSMutableArray alloc] init];

ZIMMessageDeleteConfig *config = [[ZIMMessageDeleteConfig alloc] init];
// When deleting messages for everyone, server messages must also be deleted
config.isAlsoDeleteServerMessage = YES;
config.isAlsoDeleteForEveryone = YES;

[self.zim deleteMessages:deleteMessageList conversationID:conversationID conversationType:ZIMConversationTypeGroup config:config callback:^(NSString * _Nonnull conversationID, ZIMConversationType conversationType, ZIMError * _Nonnull errorInfo) {
    // Developers can listen for whether the messages are deleted successfully through this callback.
}];

// Other members in the conversation are notified of the message deletion through this callback
- (void)zim:(ZIM *)zim messageDeleted:(ZIMMessageDeletedInfo *)deletedInfo {
    if (deletedInfo.messageDeleteType == ZIMMessageDeleteTypeMessageListDeleted) {
        // deletedInfo.messageList is the list of deleted messages, where you can refresh the UI
    }
}
Warning
  • isAlsoDeleteForEveryone only takes effect for the deleteMessages interface, and is not supported by deleteAllMessageByConversationID or deleteAllConversationMessagesWithConfig.
  • When using this configuration, isAlsoDeleteServerMessage must also be set to true.
  • A maximum of 100 messages can be deleted at a time.
  • This operation takes effect for the entire batch: as long as any message in the passed-in list does not meet the conditions (exceeding the validity period, no operation permission, etc.), the entire batch fails, and the remaining valid messages in the list will not be deleted.

Validity period

Only messages within the validity period can be deleted for everyone, and the validity period starts from the time the message is sent. The default validity period is 24 hours and can be configured to up to 7 days. To modify the configuration, contact ZEGO technical support.

It is recommended that the business layer decide whether to display the "Delete for everyone" entry to users based on the difference between the timestamp of the message and the current time. If this operation is mistakenly triggered due to inaccurate local device time, the interface returns an error code as a fallback.

Operation permissions

The scope of messages that conversation members can delete for everyone is as follows:

Conversation typeMessages that can be deleted for everyone
One-to-oneOnly the messages sent by yourself can be deleted, and deleting the messages sent by the other party is not supported.
GroupRegular members can only delete the messages sent by themselves; admins can delete the messages sent by themselves and regular members; the group owner can delete the messages sent by themselves, admins, and regular members.

In group conversations, it is recommended that the business layer first query the identities of the operator and the message sender in the group through queryGroupMemberInfoByGroupID, and then decide whether to display the "Delete for everyone" entry to users.

Note

For group targeted messages, only the target members of the message will receive the deletion notification, and non-target members are not affected.

Error codes

When the call fails, the common error codes are as follows:

Error codeDescription
6000001Parameter error. isAlsoDeleteServerMessage is not set to true at the same time, or more than 100 messages are deleted at a time.
6000288The message to be deleted has exceeded the validity period for deletion for everyone.
6000285No operation permission in the one-to-one conversation scenario, that is, an attempt is made to delete a message sent by the other party.
6000541No operation permission in the group conversation scenario, that is, an attempt is made to delete a message sent by a member whose permission is higher than or equal to your own.

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

Previous

Get message history

Next

Insert local messages

On this page

Back to top