Delete a space

This guide explains how to delete a named space with the Google Chat API. Delete a space when it's no longer needed. Deleting a space also deletes everything that it contains, including messages and attachments.

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 space requires User authentication with the chat.delete authorization scope from a user who has permission to delete the specified space.

Delete a named space

To asynchronously delete an existing space in Google Chat, pass the following in your request:

Here's how to delete a space:

Python

  1. In your working directory, create a file named chat_space_delete.py.
  2. Include the following code in chat_space_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.delete"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then deletes the specified space.
        '''
    
        # 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().delete(
    
              # The space 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.
              name='spaces/SPACE'
    
          ).execute()
    
        # Print Chat API's response in your command line interface.
        # When deleting a space, the response body is empty.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. In the code, replace SPACE with the space name, which you can obtain from the spaces.list method in the Chat API, or from a space's URL.

  4. In your working directory, build and run the sample:

    python3 chat_space_delete.py
    

If successful, the response body is empty, which indicates that the space is deleted.