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.

In the Chat API, a Chat message is represented by the Message resource. While Chat users can only send messages that contain text, Chat apps can use many other messaging features, including displaying static or interactive user interfaces, collecting information from users, and delivering messages privately. To learn more about messaging features available for the Chat API, see the 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

Node.js

Python

Java

Apps Script

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 DeleteMessage() method.
  • Set name to the resource name of the message to delete.

The following example deletes a message with user authentication:

Node.js

chat/client-libraries/cloud/delete-message-user-cred.js
import {createClientWithUserCredentials} from './authentication-utils.js';

const USER_AUTH_OAUTH_SCOPES = ['https://www.googleapis.com/auth/chat.messages'];

// This sample shows how to delete a message with user credential
async function main() {
  // Create a client
  const chatClient = await createClientWithUserCredentials(USER_AUTH_OAUTH_SCOPES);

  // Initialize request argument(s)
  const request = {
    // Replace SPACE_NAME and MESSAGE_NAME here
    name: 'spaces/SPACE_NAME/messages/MESSAGE_NAME'
  };

  // Make the request
  const response = await chatClient.deleteMessage(request);

  // Handle the response
  console.log(response);
}

main().catch(console.error);

Python

chat/client-libraries/cloud/delete_message_user_cred.py
from authentication_utils import create_client_with_user_credentials
from google.apps import chat_v1 as google_chat

SCOPES = ["https://www.googleapis.com/auth/chat.messages"]

# This sample shows how to delete a message with user credential
def delete_message_with_user_cred():
    # Create a client
    client = create_client_with_user_credentials(SCOPES)

    # Initialize request argument(s)
    request = google_chat.DeleteMessageRequest(
        # Replace SPACE_NAME and MESSAGE_NAME here
        name = "spaces/SPACE_NAME/messages/MESSAGE_NAME",
    )

    # Make the request
    response = client.delete_message(request)

    # Handle the response
    print(response)

delete_message_with_user_cred()

Java

chat/client-libraries/cloud/src/main/java/com/google/workspace/api/chat/samples/DeleteMessageUserCred.java
import com.google.chat.v1.ChatServiceClient;
import com.google.chat.v1.DeleteMessageRequest;
import com.google.chat.v1.SpaceName;

// This sample shows how to delete message with user credential.
public class DeleteMessageUserCred {

  private static final String SCOPE =
    "https://www.googleapis.com/auth/chat.messages";

  public static void main(String[] args) throws Exception {
    try (ChatServiceClient chatServiceClient =
        AuthenticationUtils.createClientWithUserCredentials(
          ImmutableList.of(SCOPE))) {
      DeleteMessageRequest.Builder request = DeleteMessageRequest.newBuilder()
        // replace SPACE_NAME and MESSAGE_NAME here
        .setName("spaces/SPACE_NAME/messages/MESSAGE_NAME");
      chatServiceClient.deleteMessage(request.build());
    }
  }
}

Apps Script

chat/advanced-service/Main.gs
/**
 * This sample shows how to delete a message with user credential
 * 
 * It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.messages'
 * referenced in the manifest file (appsscript.json).
 */
function deleteMessageUserCred() {
  // Initialize request argument(s)
  // TODO(developer): Replace SPACE_NAME and MESSAGE_NAME here
  const name = 'spaces/SPACE_NAME/messages/MESSAGE_NAME';

  // Make the request
  const response = Chat.Spaces.Messages.remove(name);

  // Handle the response
  console.log(response);
}

To run this sample, replace the following:

  • SPACE_NAME: the ID from the space's name. You can obtain the ID by calling the ListSpaces() method or from the space's URL.
  • MESSAGE_NAME: the ID from the message's name. You can obtain the ID 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.

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 DeleteMessage() method.
  • Set name to the resource name of the message to delete.

The following example deletes a message with app authentication:

Node.js

chat/client-libraries/cloud/delete-message-app-cred.js
import {createClientWithAppCredentials} from './authentication-utils.js';

// This sample shows how to delete a message with app credential
async function main() {
  // Create a client
  const chatClient = createClientWithAppCredentials();

  // Initialize request argument(s)
  const request = {
    // Replace SPACE_NAME and MESSAGE_NAME here
    name: 'spaces/SPACE_NAME/messages/MESSAGE_NAME'
  };

  // Make the request
  const response = await chatClient.deleteMessage(request);

  // Handle the response
  console.log(response);
}

main().catch(console.error);

Python

chat/client-libraries/cloud/delete_message_app_cred.py
from authentication_utils import create_client_with_app_credentials
from google.apps import chat_v1 as google_chat

# This sample shows how to delete a message with app credential
def delete_message_with_app_cred():
    # Create a client
    client = create_client_with_app_credentials()

    # Initialize request argument(s)
    request = google_chat.DeleteMessageRequest(
        # Replace SPACE_NAME and MESSAGE_NAME here
        name = "spaces/SPACE_NAME/messages/MESSAGE_NAME",
    )

    # Make the request
    response = client.delete_message(request)

    # Handle the response
    print(response)

delete_message_with_app_cred()

Java

chat/client-libraries/cloud/src/main/java/com/google/workspace/api/chat/samples/DeleteMessageAppCred.java
import com.google.chat.v1.ChatServiceClient;
import com.google.chat.v1.DeleteMessageRequest;

// This sample shows how to delete message with app credential.
public class DeleteMessageAppCred {

  public static void main(String[] args) throws Exception {
    try (ChatServiceClient chatServiceClient =
        AuthenticationUtils.createClientWithAppCredentials()) {
      DeleteMessageRequest.Builder request = DeleteMessageRequest.newBuilder()
        // replace SPACE_NAME and MESSAGE_NAME here
        .setName("spaces/SPACE_NAME/messages/MESSAGE_NAME");
      chatServiceClient.deleteMessage(request.build());
    }
  }
}

Apps Script

chat/advanced-service/Main.gs
/**
 * This sample shows how to delete a message with app credential
 * 
 * It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.bot'
 * used by service accounts.
 */
function deleteMessageAppCred() {
  // Initialize request argument(s)
  // TODO(developer): Replace SPACE_NAME and MESSAGE_NAME here
  const name = 'spaces/SPACE_NAME/messages/MESSAGE_NAME';
  const parameters = {};

  // Make the request
  const response = Chat.Spaces.Messages.remove(name, parameters, getHeaderWithAppCredentials());

  // Handle the response
  console.log(response);
}

To run this sample, replace the following:

  • SPACE_NAME: the ID from the space's name. You can obtain the ID by calling the ListSpaces() method or from the space's URL.
  • MESSAGE_NAME: the ID from the message's name. You can obtain the ID 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.

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