please select
  • UIKit
  • SDK
  • Server APIs
Chat/
SDK/
Android/
Message/
SDK
  • Install Chat SDK
  • Initialize Chat SDK
  • Login and Logout
  • Message
    • Overview
    • Send a Message
    • Receive a Message
    • Retrieve Messages
    • Forward Messages
    • Modify a Message
    • Insert a Message
    • Delete Messages
    • Clear History Messages
    • Recall a Message
    • Send an Online Message
    • Message Read Receipt
    • Query Messages
    • Mentions
    • Targeted Group Message
    • Do not Notify
    • Key-Value Extensions
    • Reactions
    • Translation
    • Pin Messages
  • Conversation
    • Overview
    • Conversation List
    • Get Conversations
    • Unread Count
    • Pin Conversations
    • Delete Conversations
    • Draft
    • Mark
    • Conversation Group
  • Group
    • Overview
    • Manage Group
    • Profile
    • Manage Members
    • Member Profile
    • Attribute
    • Counter
  • Community and Topic
    • Manage Community
    • Permission Group
  • User
    • User Profile
    • User Status
    • Manage Friends
    • Friend Group
    • Block Lists
    • Follow
  • Local Search
    • Search Messages
    • Search Friends
    • Search Groups
    • Search Group Members
  • Signaling
  • API Reference
    • Java
  • Guideline for Beginners
  • Console Guide
    • Creating and Upgrading an Application
    • Basic Configuration
    • Feature Configuration
    • Account Management
    • Group Management
    • Webhook Configuration
  • Product Introduction
    • Message Management
      • One-to-One Message
      • Message Storage
      • Offline Push
      • Group Message
      • Message Formats
    • Account System
      • Login Authentication
      • Online Status Management
    • Group Related
      • Group System
      • Group Management
    • User Profile and Relationship Chain
      • Profile Management
      • Relationship Chain Management
  • Purchase Guide
    • Billing Overview
    • Pricing
  • Error Codes

Delete Messages

Feature Description

Both local messages and cloud messages can be deleted. When cloud messages are deleted, such messages will be deleted both locally and from the cloud and cannot be recovered.
If the last message is deleted, the lastMessage in the conversation will become the last but one message.
If your SDK version is earlier than v5.5.892 and the lastMessage is used for sorting, the sequence in the conversation list will be affected.
If your SDK is on v5.5.892 or later and orderKey is used for sorting, the sequence in the conversation list will not be affected.
See Conversation List for details.

Deleting a local message

Call deleteMessageFromLocalStorage (Android / iOS and macOS) to delete a local message.
Note
1. This API can only be used to delete a historical local message. After deleted, the message will be marked as deleted locally by the SDK and cannot be pulled through getHistoryMessage.
2. If the application is uninstalled and reinstalled, the delete marker will be lost locally, and the message can still be pulled through getHistoryMessage.
Sample code:
Android
iOS and macOS
// `selectedMsg` is the selected message to be deleted.
V2TIMManager.getMessageManager().deleteMessageFromLocalStorage(selectedMsg, new V2TIMCallback() {
@Override
public void onSuccess() {
// Local message deleted successfully
}

@Override
public void onError(int code, String desc) {
// Failed to delete the local message
}
});
// `selectedMsg` is the selected message to be deleted.
[[V2TIMManager sharedInstance] deleteMessageFromLocalStorage:selectedMessage
succ:^{
NSLog(@"Local message deleted successfully");
} fail:^(int code, NSString *msg) {
NSLog(@"Failed to delete the local message, code: %d, desc: %@", code, msg);
}];

Deleting a message from the cloud

Call deleteMessages (Android / iOS and macOSWindows) to delete messages from the cloud.
This API deletes messages both locally and from the cloud, which cannot be recovered.
Note
1. Up to 50 messages can be deleted per call.
2. Messages to be deleted per call must be from the same conversation.
3. This API can be called only once per second.
4. If messages have been pulled on a device by an account, they will remain on the device after the API is called to delete them from the cloud; in other words, deleted messages are not synced.
Sample code:
Android
iOS and macOS
Windows
// `selectedMessageList` is the list of selected messages to be deleted.
V2TIMManager.getMessageManager().deleteMessages(selectedMessageList, new V2TIMCallback() {
@Override
public void onSuccess() {
// Messages deleted from the cloud successfully
}

@Override
public void onError(int code, String desc) {
// Failed to delete the messages from the cloud
}
});
// `selectedMessageList` is the list of selected messages to be deleted.
NSArray *selectedMessageList = @[selectedMessage1, selectedMessage2];
[[V2TIMManager sharedInstance] deleteMessages:selectedMessageList
succ:^{
NSLog(@"Messages deleted from the cloud successfully");
} fail:^(int code, NSString *desc) {
NSLog(@"Failed to delete the messages from the cloud, code: %d, desc: %@", code, desc);
}];
class Callback final : public V2TIMCallback {
public:
using SuccessCallback = std::function<void()>;
using ErrorCallback = std::function<void(int, const V2TIMString&)>;

Callback() = default;
~Callback() override = default;

void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {
success_callback_ = std::move(success_callback);
error_callback_ = std::move(error_callback);
}

void OnSuccess() override {
if (success_callback_) {
success_callback_();
}
}
void OnError(int error_code, const V2TIMString& error_message) override {
if (error_callback_) {
error_callback_(error_code, error_message);
}
}

private:
SuccessCallback success_callback_;
ErrorCallback error_callback_;
};

// `messageList` is the list of selected messages to be deleted.
auto callback = new Callback;
callback->SetCallback(
[=]() {
// Messages deleted from the cloud successfully
delete callback;
},
[=](int error_code, const V2TIMString& error_message) {
// Failed to delete the messages from the cloud
delete callback;
});

V2TIMManager::GetInstance()->GetMessageManager()->DeleteMessages(messageList, callback);