getstream-io

How to delete user from channel


When trying to delete a user, an error message is displayed:

Error removing user from conversation: Error: StreamChat error code 4: UpdateChannel failed with error: "cannot add or remove members in a distinct channel, please create a new distinct channel with the desired members"

Below is a sample code to delete a user:

try {
      const response = await conversation.removeMembers(
        [user.id],
        { text: `${user.name} was removed from conversation`}
      );

      console.log('Response: ', response);

      console.log(`${user.name} was removed from conversation`);
    } catch (e) {
      console.log(`Error removing user from conversation: ${e}`);
    }

Conversation: enter image description here

On deletion, the first request returns OK status with the conversation enter image description here


Solution

  • We ran into this issue also. Had to change the way that were creating channels. The docs make this really hard to find but on this page it says You cannot add/remove members for channels created this way.

    Meaning if you want to create a channel in which members can be added and removed you have to create it like shown here where you assign the channel a unique ID that you create (we are using a UUID). Once it's been created you can add your desired members to it... and then later add or remove them.