Remove a member from a space (original) (raw)

This guide explains how to use thedelete()method on the Membership resource of the Google Chat API to remove members from a space, also known as deleting a membership. Space managers can't be removed if they're the only space manager in a space. Assign another user as a space manager before removing these memberships.

If you're a Google Workspace administrator, you can remove users, Google Groups, or Chat apps from any space in your Google Workspace organization.

TheMembership resourcerepresents whether a human user or Google Chat app is invited to, part of, or absent from a space.

Prerequisites

Node.js

To remove a user, Google Group, or Chat app from a space withuser authentication, pass the following in your request:

Here's how to delete a membership withuser authentication:

Node.js

To run this sample, replace the following:

If successful, the response body returns the membership with'state': 'NOT_A_MEMBER', indicating that the member is no longer in the space.

{ "name": "spaces/SPACE_NAME/members/MEMBER_NAME", "state": "NOT_A_MEMBER" }

Remove a member from a space as a Chat app

App authentication requires one-timeadministrator approval.

To remove a user, Google Group, or Chat app from a space withapp authentication, pass the following in your request:

Write a script that calls Chat API

Here's how to delete a membership withapp authentication:

Python

  1. In your working directory, create a file named chat_membership_delete_app.py.
  2. Include the following code in chat_membership_delete_app.py:
from google.oauth2 import service_account  
from apiclient.discovery import build  
# Define your app's authorization scopes.  
# When modifying these scopes, delete the file token.json, if it exists.  
SCOPES = ["https://www.googleapis.com/auth/chat.app.memberships"]  
def main():  
    '''  
    Authenticates with Chat API using app authentication,  
    then deletes the specified membership.  
    '''  
    # Specify service account details.  
    creds = (  
        service_account.Credentials.from_service_account_file('credentials.json')  
        .with_scopes(SCOPES)  
    )  
    # Build a service endpoint for Chat API.  
    chat = build('chat', 'v1', credentials=creds)  
    # Use the service endpoint to call Chat API.  
    result = chat.spaces().members().delete(  
        # The membership to delete.  
        #  
        # Replace SPACE with a space name.  
        # Obtain the space name from the spaces resource of Chat API,  
        # or from a space's URL.  
        #  
        # Replace MEMBER with a membership name.  
        # Obtain the membership name from the memberships resource of  
        # Chat API. To delete a Chat app's membership, replace MEMBER  
        # with app; an alias for the app calling the API.  
        name='spaces/SPACE/members/MEMBER'  
    ).execute()  
    # Print Chat API's response in your command line interface.  
    # When deleting a membership, the response body is empty.  
    print(result)  
if __name__ == '__main__':  
    main()  
  1. In the code, replace the following:
    • SPACE: a space name, which you can obtain from the spaces.list methodin the Chat API, or from a space's URL.
    • MEMBER: a membership name, which you can obtain from the spaces.members.list methodin the Chat API. To delete an app's membership, replaceMEMBER with app.
  2. In your working directory, build and run the sample:
python3 chat_membership_delete_app.py  

If successful, the response body returns the membership with'state': 'NOT_A_MEMBER', indicating that the member is no longer in the space.

{ "name": "spaces/SPACE/members/MEMBER", "state": "NOT_A_MEMBER" }

Remove users or Google Groups from a space as a Google Workspace administrator

If you're a Google Workspace administrator, you can call theDeleteMembership() method to remove users, Google Groups, or Chat apps from any space in your Google Workspace organization.

To call this method as a Google Workspace administrator, do the following:

For more information and examples, seeManage Google Chat spaces as a Google Workspace administrator.

Limitations and considerations