This guide explains how to use the delete()
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.
The
Membership
resource
represents whether a human user or Google Chat app is invited to,
part of, or absent from a space.
Prerequisites
Python
- A Business or Enterprise Google Workspace account with access to Google Chat.
- Set up your environment:
- Create a Google Cloud project.
- Configure the OAuth consent screen.
- Enable and configure the Google Chat API with a name, icon, and description for your Chat app.
- Install the Python Google API Client Library.
-
Create OAuth client ID credentials for a desktop application. To run the sample in this
guide, save the credentials as a JSON file named
client_secrets.json
to your local directory.
- Choose an authorization scope that supports user authentication.
Remove a member from a space
To remove a user, Google Group, or Chat app from a space:
- To remove a user or Google Group, specify the
chat.memberships
authorization scope. To remove a Chat app, specify thechat.memberships.app
authorization scope (apps can only delete their own membership; not that of other apps). As a best practice, choose the most restrictive scope that still allows your app to function. - Call the
delete()
method on themembership
resource. - Pass the
name
of the membership to delete. If the membership belongs to the only space manager in a space, assign another user as a space manager before deleting this membership.
Here's how to delete a membership:
Python
- In your working directory, create a file named
chat_membership_delete.py
. Include the following code in
chat_membership_delete.py
:from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.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.memberships.app"] def main(): ''' Authenticates with Chat API via user credentials, then deletes the specified membership. ''' # Authenticate with Google Workspace # and get user authorization. flow = InstalledAppFlow.from_client_secrets_file( 'client_secrets.json', SCOPES) creds = flow.run_local_server() # 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()
In the code, replace the following:
SPACE
: a space name, which you can obtain from thespaces.list
method in the Chat API, or from a space's URL.MEMBER
: a membership name, which you can obtain from thespaces.members.list
method in the Chat API. To delete an app's membership, replaceMEMBER
withapp
.
In your working directory, build and run the sample:
python3 chat_membership_delete.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 the delete()
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:
- Call the method using user authentication, and specify an authorization scope that supports calling the method using administrator privileges.
- In your request, specify the query parameter
useAdminAccess
totrue
.
For more information and examples, see Manage Google Chat spaces as a Google Workspace administrator.
Related topics
- Get details about a user's or Chat app's membership.
- List members in a space.
- Update a user's membership in a Google Chat space.
- Invite or add a user or Chat app to a space.