> ## Documentation Index
> Fetch the complete documentation index at: https://cometchat-22654f5b-docs-ios-ui-kit-sdk-fixes.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Leave A Group

> Guide to leaving groups using the CometChat iOS SDK leaveGroup method to stop receiving group messages.

<Info>
  **Quick Reference for AI Agents & Developers**

  * **Leave group:** `CometChat.leaveGroup(GUID:onSuccess:onError:)`
  * **Note:** Group owner cannot leave — must transfer ownership first
  * **Related:** [Join Group](/sdk/ios/join-group) · [Transfer Ownership](/sdk/ios/transfer-group-ownership) · [Groups Overview](/sdk/ios/groups-overview)
</Info>

## Leave a Group

In order to stop receiving updates and messages for any particular joined group, you will have to leave the group using the `leaveGroup()` method.

### Leave Group Parameters

| Parameter | Type   | Description                      |
| --------- | ------ | -------------------------------- |
| GUID      | String | Unique group identifier to leave |

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    let guid = "cometchat-guid-11"

    CometChat.leaveGroup(GUID: guid, onSuccess: { (response) in
        print("Left group successfully.")
    }, onError: { (error) in
        print("Group leaving failed with error:" + error!.errorDescription)
    })
    ```
  </Tab>

  <Tab title="Objective C">
    ```objc theme={null}
    NSString *guid = @"cometchat-guid-101";

    [CometChat leaveGroupWithGUID:guid onSuccess:^(NSString * response) {
        NSLog(@"Left group successfully. %@", response);
    } onError:^(CometChatException * error) {
        NSLog(@"Group leaving failed with error: %@", [error errorDescription]);
    }];
    ```
  </Tab>
</Tabs>

<Accordion title="Sample Payload - Leave Group">
  **Request Parameters:**

  | Parameter | Type   | Description                                                     |
  | --------- | ------ | --------------------------------------------------------------- |
  | GUID      | String | Unique group identifier to leave. Example: `"cometchat-guid-1"` |

  **Success Response:**

  | Parameter | Type   | Description                                            |
  | --------- | ------ | ------------------------------------------------------ |
  | response  | String | Success message. Example: `"Group left successfully."` |

  **After Leaving Group:**

  | Effect      | Description                                              |
  | ----------- | -------------------------------------------------------- |
  | Messages    | User will NOT receive messages from this group           |
  | Member List | User will NOT appear in group member list                |
  | hasJoined   | Will be `false` if group is fetched again                |
  | Rejoin      | User can rejoin if group is public or password-protected |

  **Error Response ([CometChatException](#common-error-codes)):**

  | Parameter        | Type   | Description                                                                                                                                                               |
  | ---------------- | ------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
  | errorCode        | String | Unique error code. Example: `"ERR_GROUP_NOT_JOINED"`                                                                                                                      |
  | errorDescription | String | Human-readable error message. Example: `"The user with UID cometchat-uid-2 is not a member of the group with GUID cometchat-guid-1. Please join the group to access it."` |
</Accordion>

<Accordion title="Sample Payload - Leave Group (Owner Error)">
  **Request Parameters:**

  | Parameter | Type   | Description                                                     |
  | --------- | ------ | --------------------------------------------------------------- |
  | GUID      | String | Unique group identifier to leave. Example: `"cometchat-guid-1"` |

  **Error Response:**

  | Parameter        | Type   | Description                                                                                         |
  | ---------------- | ------ | --------------------------------------------------------------------------------------------------- |
  | errorCode        | String | Unique error code. Example: `"ERR_OWNER_CANNOT_LEAVE"`                                              |
  | errorDescription | String | Human-readable error message. Example: `"Group owner cannot leave - must transfer ownership first"` |

  <Warning>Group owner CANNOT leave the group. Owner must transfer ownership first using `transferGroupOwnership()`.</Warning>
</Accordion>

Once a group is left, the user will not receive any updates or messages pertaining to the group.

<Warning>
  Group owner CANNOT leave the group. Owner must transfer ownership first using [transferGroupOwnership()](/sdk/ios/transfer-group-ownership).
</Warning>

***

## Real-time Leave Group Event

*In other words, as a member of a group, how do I know if someone has left it?*

If a user leaves any group, the members of the group receive a real-time event in the `onGroupMemberLeft()` method of the `CometChatGroupDelegate`. In order to receive user Events, you must add protocol conformance `CometChatGroupDelegate` as shown below:

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    class ViewController: UIViewController, CometChatGroupDelegate {
        
        override func viewDidLoad() {
            super.viewDidLoad()
            CometChat.groupdelegate = self
        }
        
        func onGroupMemberLeft(action: ActionMessage, leftUser: User, leftGroup: Group) {
            print("\(leftUser.name ?? "") left the group \(leftGroup.name ?? "").")
        }
    }
    ```
  </Tab>

  <Tab title="Objective C">
    ```objc theme={null}
    @interface ViewController ()<CometChatGroupDelegate>
    @end

    @implementation ViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
        [CometChat setGroupdelegate:self];
    }

    - (void)onGroupMemberLeftWithAction:(ActionMessage *)action leftUser:(User *)leftUser leftGroup:(Group *)leftGroup {
        // User left the group
    }

    @end
    ```
  </Tab>
</Tabs>

<Accordion title="Sample Payload - onGroupMemberLeft Event">
  **Event Trigger:** Received via `CometChatGroupDelegate.onGroupMemberLeft(action:leftUser:leftGroup:)`

  **ActionMessage Object:**

  | Parameter | Type                                               | Description                                                                      |
  | --------- | -------------------------------------------------- | -------------------------------------------------------------------------------- |
  | action    | String                                             | Action type. Example: `"left"`                                                   |
  | actionBy  | [User](/sdk/ios/users-overview#user-properties)    | User who performed the action. Example: `{"uid": "user123", "name": "John"}`     |
  | actionFor | [Group](/sdk/ios/retrieve-groups#group-properties) | Group where action occurred. Example: `{"guid": "group123", "name": "My Group"}` |

  **leftUser ([User](/sdk/ios/users-overview#user-properties) Object):**

  | Parameter | Type                                                  | Description                                                                 |
  | --------- | ----------------------------------------------------- | --------------------------------------------------------------------------- |
  | uid       | String?                                               | Unique identifier of the user who left. Example: `"user123"`                |
  | name      | String?                                               | Display name of the user. Example: `"John Doe"`                             |
  | avatar    | String?                                               | URL to the user's avatar image. Example: `"https://example.com/avatar.png"` |
  | status    | [UserStatus](/sdk/ios/retrieve-users#userstatus-enum) | Current online status. Example: `.online`                                   |

  **leftGroup ([Group](/sdk/ios/retrieve-groups#group-properties) Object):**

  | Parameter    | Type    | Description                                       |
  | ------------ | ------- | ------------------------------------------------- |
  | guid         | String  | Unique group identifier. Example: `"group123"`    |
  | name         | String? | Group display name. Example: `"My Group"`         |
  | membersCount | Int     | Updated member count (decremented). Example: `14` |
</Accordion>

Do not forget to set your view controller as a CometChat delegate probably in `viewDidLoad()` as `CometChat.groupdelegate = self`

## Missed Group Member Left Events

*In other words, as a member of a group, how do I know if someone has left it when my app is not running?*

When you retrieve the list of previous messages if a member has left any group that the logged-in user is a member of, the list of messages will contain an `Action` message. An `Action` message is a sub-class of `BaseMessage` class.

For the group member left event, in the `Action` object received, the following fields can help you get the relevant information:

| Field     | Value        | Description             |
| --------- | ------------ | ----------------------- |
| action    | `"left"`     | Action type             |
| actionBy  | User object  | User who left the group |
| actionFor | Group object | Group the user left     |

***

## CometChatGroupDelegate

Listen for real-time group events by conforming to `CometChatGroupDelegate`.

### Delegate Methods

| Method                      | Parameters                                                   | Description     |
| --------------------------- | ------------------------------------------------------------ | --------------- |
| `onMemberAddedToGroup`      | action, addedBy, addedUser, addedTo                          | Member added    |
| `onGroupMemberLeft`         | action, leftUser, leftGroup                                  | Member left     |
| `onGroupMemberJoined`       | action, joinedUser, joinedGroup                              | Member joined   |
| `onGroupMemberKicked`       | action, kickedUser, kickedBy, kickedFrom                     | Member kicked   |
| `onGroupMemberBanned`       | action, bannedUser, bannedBy, bannedFrom                     | Member banned   |
| `onGroupMemberUnbanned`     | action, unbannedUser, unbannedBy, unbannedFrom               | Member unbanned |
| `onGroupMemberScopeChanged` | action, updatedUser, scopeChangedTo, scopeChangedFrom, group | Scope changed   |
| `onOwnershipChanged`        | group, newOwner                                              | Owner changed   |

<Accordion title="Sample Payload - Start Listening for Group Events">
  **Setup Configuration:**

  | Parameter     | Type                   | Description                                       |
  | ------------- | ---------------------- | ------------------------------------------------- |
  | groupdelegate | CometChatGroupDelegate | Reference to the delegate object. Example: `self` |

  **Listening Status:**

  | Parameter | Type   | Description                                                      |
  | --------- | ------ | ---------------------------------------------------------------- |
  | status    | String | Current listening state. Example: `"Listening for group events"` |

  **Events Being Monitored:**

  | Event                     | Description              |
  | ------------------------- | ------------------------ |
  | onMemberAddedToGroup      | When member is added     |
  | onGroupMemberLeft         | When member leaves       |
  | onGroupMemberJoined       | When member joins        |
  | onGroupMemberKicked       | When member is kicked    |
  | onGroupMemberBanned       | When member is banned    |
  | onGroupMemberUnbanned     | When member is unbanned  |
  | onGroupMemberScopeChanged | When scope changes       |
  | onOwnershipChanged        | When ownership transfers |
</Accordion>

<Accordion title="Sample Payload - Stop Listening for Group Events">
  **Cleanup Configuration:**

  | Parameter     | Type                    | Description                                         |
  | ------------- | ----------------------- | --------------------------------------------------- |
  | groupdelegate | CometChatGroupDelegate? | Set to nil to stop receiving events. Example: `nil` |

  **Listening Status:**

  | Parameter | Type   | Description                                                                |
  | --------- | ------ | -------------------------------------------------------------------------- |
  | status    | String | Current listening state. Example: `"No longer listening for group events"` |
</Accordion>

<Warning>
  * Set delegate in `viewDidLoad()`: `CometChat.groupdelegate = self`
  * Remove delegate when view is dismissed to avoid memory leaks
</Warning>

***

## Common Error Codes

| Error Code                | Description                | Resolution                                                |
| ------------------------- | -------------------------- | --------------------------------------------------------- |
| ERR\_NOT\_LOGGED\_IN      | User is not logged in      | Login first using `CometChat.login()`                     |
| ERR\_GUID\_NOT\_FOUND     | Group does not exist       | Verify the GUID is correct                                |
| ERR\_GROUP\_NOT\_JOINED   | Not a member of this group | User must be a member to leave                            |
| ERR\_OWNER\_CANNOT\_LEAVE | Owner cannot leave group   | Transfer ownership first using `transferGroupOwnership()` |
