Delete a membership

This guide explains how to delete a membership with the Google Chat API. Delete a membership to remove a user or app from a space.

Prerequisites

Python

  • Python 3.6 or greater
  • The pip package management tool
  • The Google client libraries for Python. To install them, run the following command in your command-line interface:

    pip3 install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib oauth2client
    
  • A published Chat app. To create and publish a Chat app, see Build a Google Chat app.

  • Authorization configured for the Chat app. Deleting a membership requires User authentication with the chat.memberships or chat.memberships.app authorization scope from a user who has permission to delete the specified membership.

Delete a membership

To delete an existing membership in Google Chat:

  • To delete a human membership, specify the chat.memberships authorization scope. To delete an app membership, specify the chat.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 the Member resource.
  • Pass the name of the membership to delete.

Here's how to delete a membership:

Python

  1. In your working directory, create a file named chat_membership_delete.py.
  2. Include the following code in chat_membership_delete.py:

    import os.path
    
    from google.auth.transport.requests import Request
    from google.oauth2.credentials import Credentials
    from google_auth_oauthlib.flow import InstalledAppFlow
    from googleapiclient.discovery import build
    from googleapiclient.errors import HttpError
    
    # 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()
    
  3. In the code, replace the following:

    • SPACE: a space name, which you can obtain from the spaces.list method in the Chat API, or from a space's URL.

    • MEMBER: a membership name, which you can obtain from the spaces.members.list method in the Chat API. To delete an app's membership, replace MEMBER with app.

  4. 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"
}