Skip to main content

Delete messages

Use the channel message deletion APIs to remove specific messages, delete messages up to a timestamp, or recall a sent message for all participants.

tip
  • BaseChannel.deleteLocalMessages() removes local messages by client IDs only.
  • channel.deleteMessagesForMe() deletes the specified messages for the current user. The wrapper deletes remotely first, then removes the same messages locally.
  • channel.deleteMessagesForMeByTimestamp() deletes messages in the current channel up to a timestamp for the current user.
  • BaseChannel.deleteMessageForAll() recalls a sent message for all participants and returns the recalled Message in the callback when available.
note

Use the callback to read the final result. For ErrorHandler, success means error?.isSuccess == true.

Delete specific messages (local only)

Delete one or more messages from the local database.

Method

Dart
static Future<int> deleteLocalMessages(List<int> messageClientIds, ErrorHandler handler)

Code example

Dart
await BaseChannel.deleteLocalMessages(
[12345, 12346],
(NCError? error) {
if (error?.isSuccess == true) {
print('Messages deleted locally');
}
},
);

Delete specific messages for the current user

Delete one or more messages in the current channel for the current user.

Method

Dart
Future<int> deleteMessagesForMe(List<Message> messages, ErrorHandler handler)
Dart
final channel = DirectChannel('<target-user-id>');
await channel.deleteMessagesForMe(
messages,
(error) {
if (error?.isSuccess == true) {
print('Messages deleted');
}
},
);

Delete messages by timestamp for the current user

Delete messages in the current channel up to a specified timestamp for the current user.

Method

Dart
Future<int> deleteMessagesForMeByTimestamp(DeleteMessagesForMeByTimestampParams params, ErrorHandler handler)

Code example

Dart
final channel = DirectChannel('<target-user-id>');
await channel.deleteMessagesForMeByTimestamp(
DeleteMessagesForMeByTimestampParams(
timestamp: <cutoff-timestamp>, // 0 to delete all messages for the current user
policy: MessageOperationPolicy.localRemote,
),
(error) {
if (error?.isSuccess == true) {
print('Messages deleted');
}
},
);

DeleteMessagesForMeByTimestampParams

ParameterTypeDefaultDescription
timestampintRequiredMessages sent before this timestamp are deleted for the current user.
policyMessageOperationPolicyMessageOperationPolicy.localScope of the delete operation: local, remote, or localRemote.

Recall a message for all participants

Dart
await BaseChannel.deleteMessageForAll(
message,
(recalledMessage, error) {
if (error?.isSuccess == true) {
print('Message recalled: ${recalledMessage?.messageId}');
}
},
);