deleteConversation static method

Future<void> deleteConversation(
  1. String conversationWith,
  2. String conversationType, {
  3. required dynamic onSuccess(
    1. String result
    )?,
  4. required dynamic onError(
    1. CometChatException excep
    )?,
})

Method deletes the conversation only for the logged-in user.

conversationWith The ID of the message above which all messages for a particular conversation are to be marked as read.

conversationType The type of conversation you want to delete . It can be either user or group

Migration Note: Migrated from platform channels to native Dart implementation. Uses ConversationRepository for deleting conversations. Behavior and signature remain identical for backward compatibility.

Android Reference: ConversationsRequest.deleteConversation()

method could throw PlatformException with error codes specifying the cause

Implementation

static Future<void> deleteConversation(
  String conversationWith,
  String conversationType, {
  required Function(String message)? onSuccess,
  required Function(CometChatException excep)? onError,
}) async {
  try {
    // Get SDK instance
    final sdk = SdkRegistry.getInstance();

    // Call appropriate repository method based on conversation type
    if (conversationType == 'user') {
      await sdk.conversations.deleteUserConversation(conversationWith);
    } else {
      await sdk.conversations.deleteGroupConversation(conversationWith);
    }

    // E4: purge the conversation from the cache on success. Resolve the
    // canonical conversationId from the bare peer id + type (the cache keys by
    // composite id, Fix 1), then destroy its ENTIRE footprint — SENT history,
    // the queued outbox sends, and their media (C-05 / BEH-SYNC-17). Each
    // PENDING / SENDING send fails with DISCARDED once the delete commits; a
    // FAILED one goes silently (R49). Watermark untouched.
    final store = OfflineRuntime.instance.store;
    if (store != null) {
      final convId = conversationType == 'group'
          ? OfflineConversationKey.forGroup(conversationWith)
          : () {
              final self = sdk.auth.getLoggedInUser()?.uid;
              return (self == null || self.isEmpty)
                  ? null
                  : OfflineConversationKey.forUser(self, conversationWith);
            }();
      if (convId != null) {
        // A pinned conversation survives its own delete (BEH-CPIN-07), minus
        // the badge, the read marker and — for a 1:1 — the preview.
        await OfflineRuntime.instance.purgeConversation(
          convId,
          afterOwnDelete: true,
        );
      }
    }

    final result = 'Conversation deleted successfully';
    if (onSuccess != null) onSuccess(result);
  } on SdkException catch (sdkEx) {
    // Convert SdkException to CometChatException
    final cometChatEx = CometChatException(
      sdkEx.code,
      sdkEx.details ?? sdkEx.message,
      sdkEx.message,
    );
    _errorCallbackHandler(cometChatEx, null, null, onError);
  } catch (e) {
    _errorCallbackHandler(null, null, e, onError);
  }
}