deleteGroup static method

Future<void> deleteGroup(
  1. String guid, {
  2. required dynamic onSuccess(
    1. String result
    )?,
  3. required dynamic onError(
    1. CometChatException excep
    )?,
})

Deletes a group.

Migration Note: Migrated from platform channels to native Dart. Behavior and signature remain identical.

Android Reference: GroupsRequest.deleteGroup(String guid)

In order to delete a group.

The user must be an Admin of the group they are trying to delete.

guid A unique identifier for a group that logged in user wants to delete.

method could throw PlatformException with error codes specifying the cause

Implementation

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

    // Call repository
    await sdk.groups.deleteGroup(guid);

    // Own deleteGroup (BEH-MUT-13): the same own-removal purge as a leave —
    // member, banned, groups and threads snapshots, saves and thread
    // follows, messages, media, unsent sends (DISCARDED, BEH-SYNC-17) — plus
    // the conversation row even if pinned, since the group is gone. Only on
    // server success; awaited before onSuccess, as leaveGroup is. The hook
    // swallows its own failures; the guard below keeps a server-confirmed
    // delete from ever reaching onError because of the cache.
    try {
      await OfflineRuntime.instance.onOwnGroupRemoval(
        guid,
        groupDeleted: true,
      );
    } catch (_) {
      // Best-effort: the next online read corrects a surviving row.
    }

    // Call success callback
    if (onSuccess != null) onSuccess('Group deleted successfully');
  } 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) {
    // Handle unexpected errors
    _errorCallbackHandler(null, null, e, onError);
  }
}