Delete a message

This guide explains how to use the delete method on the Message resource of the Google Chat API to delete a text or card message.

The Message resource represents a text or card message in Google Chat. You can create, get, update, or delete a message in the Google Chat API by calling corresponding methods. To learn more about text and card messages, see Google Chat messages overview.

With app authentication, you can use this method to delete a message that the Chat app sent. With user authentication, you can use this method to delete a message that the authenticated user sent. If the user is a space manager for the space, you might also be able to delete a message that other space members sent. For more information, see Learn about your role as a Space Manager.

Prerequisites

Python

  • Python 3.6 or greater
  • The pip package management tool
  • The latest Google client libraries. To install or update them, run the following command in your command-line interface:
    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    

Delete a message with user authentication

To delete a message with user authentication, pass the following in your request:

  • Specify the chat.messages authorization scope.
  • Call the delete method on the Message resource.
  • Set name to the resource name of the message to delete.

The following example deletes a message with user authentication:

Python

  1. In your working directory, create a file named chat_message_delete_user.py.
  2. Include the following code in chat_message_delete_user.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.messages"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then deletes a message.
        '''
    
        # 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().messages().delete(
    
            # The message 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 MESSAGE with a message name.
            # Obtain the message name from the response body returned
            # after creating a message asynchronously with Chat REST API.
            name = 'spaces/SPACE/messages/MESSAGE'
    
        ).execute()
    
        # Prints response to the Chat API call.
        # When deleting a message, 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.
    • MESSAGE: a message name, which you can obtain from the response body returned after creating a message asynchronously with the Chat API, or with the custom name assigned to the message at creation.
  4. In your working directory, build and run the sample:

    python3 chat_message_delete_user.py
    

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

Delete a message with app authentication

To delete a message with app authentication, pass the following in your request:

  • Specify the chat.bot authorization scope.
  • Call the delete method on the Message resource.
  • Set name to the resource name of the message to delete.

The following example deletes a message with app authentication:

Python

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

    from google.oauth2 import service_account
    from apiclient.discovery import build
    
    # Specify required scopes.
    SCOPES = ['https://www.googleapis.com/auth/chat.bot']
    
    # Specify service account details.
    CREDENTIALS = (
        service_account.Credentials.from_service_account_file('credentials.json')
        .with_scopes(SCOPES)
    )
    
    # Build the URI and authenticate with the service account.
    chat = build('chat', 'v1', credentials=CREDENTIALS)
    
    # Delete a Chat message.
    result = chat.spaces().messages().delete(
    
      # The message 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 MESSAGE with a message name.
      # Obtain the message name from the response body returned
      # after creating a message asynchronously with Chat REST API.
      name='spaces/SPACE/messages/MESSAGE'
    
    ).execute()
    
    # Print Chat API's response in your command line interface.
    # When deleting a message, the response body is empty.
    print(result)
    
  3. In the code, replace the following:

    • SPACE: the name of the space where the message is posted, which you can obtain from the spaces.list method in the Chat API, or from a space's URL.
    • MESSAGE: the message name, which you can obtain from the response body returned after creating a message asynchronously with the Chat API, or with the custom name assigned to the message at creation.
  4. In your working directory, build and run the sample:

    python3 chat_delete_message_app.py
    

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