Delete a message

  • This guide demonstrates how to delete text or card messages in Google Chat using the delete() method of the Google Chat API.

  • You can delete messages sent by the Chat app or the authenticated user, and potentially messages sent by other space members if you are a space manager.

  • Before using this method, you need a Google Workspace account, a Google Cloud project, and appropriate authorization credentials (user or app).

  • Code samples are provided for Node.js, Python, Java, and Apps Script, showing how to delete messages with both user and app authentication.

  • To successfully delete a message, you need the name of the space and the name of the message you want to delete.

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';
constUSER_AUTH_OAUTH_SCOPES=[
'https://www.googleapis.com/auth/chat.messages',
];
// This sample shows how to delete a message with user credential
asyncfunctionmain(){
// Create a client
constchatClient=awaitcreateClientWithUserCredentials(
USER_AUTH_OAUTH_SCOPES,
);
// Initialize request argument(s)
constrequest={
// Replace SPACE_NAME and MESSAGE_NAME here
name:'spaces/SPACE_NAME/messages/MESSAGE_NAME',
};
// Make the request
constresponse=awaitchatClient.deleteMessage(request);
// Handle the response
console.log(response);
}
awaitmain();

Python

chat/client-libraries/cloud/delete_message_user_cred.py
fromauthentication_utilsimport create_client_with_user_credentials
fromgoogle.appsimport chat_v1 as google_chat
SCOPES = ["https://www.googleapis.com/auth/chat.messages"]
# This sample shows how to delete a message with user credential
defdelete_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
importcom.google.chat.v1.ChatServiceClient;
importcom.google.chat.v1.DeleteMessageRequest;
importcom.google.chat.v1.SpaceName;
// This sample shows how to delete message with user credential.
publicclass DeleteMessageUserCred{
privatestaticfinalStringSCOPE=
"https://www.googleapis.com/auth/chat.messages";
publicstaticvoidmain(String[]args)throwsException{
try(ChatServiceClientchatServiceClient=
AuthenticationUtils.createClientWithUserCredentials(
ImmutableList.of(SCOPE))){
DeleteMessageRequest.Builderrequest=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).
 */
functiondeleteMessageUserCred(){
// Initialize request argument(s)
// TODO(developer): Replace SPACE_NAME and MESSAGE_NAME here
constname='spaces/SPACE_NAME/messages/MESSAGE_NAME';
// Make the request
constresponse=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
asyncfunctionmain(){
// Create a client
constchatClient=createClientWithAppCredentials();
// Initialize request argument(s)
constrequest={
// Replace SPACE_NAME and MESSAGE_NAME here
name:'spaces/SPACE_NAME/messages/MESSAGE_NAME',
};
// Make the request
constresponse=awaitchatClient.deleteMessage(request);
// Handle the response
console.log(response);
}
awaitmain();

Python

chat/client-libraries/cloud/delete_message_app_cred.py
fromauthentication_utilsimport create_client_with_app_credentials
fromgoogle.appsimport chat_v1 as google_chat
# This sample shows how to delete a message with app credential
defdelete_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
importcom.google.chat.v1.ChatServiceClient;
importcom.google.chat.v1.DeleteMessageRequest;
// This sample shows how to delete message with app credential.
publicclass DeleteMessageAppCred{
publicstaticvoidmain(String[]args)throwsException{
try(ChatServiceClientchatServiceClient=
AuthenticationUtils.createClientWithAppCredentials()){
DeleteMessageRequest.Builderrequest=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.
 */
functiondeleteMessageAppCred(){
// Initialize request argument(s)
// TODO(developer): Replace SPACE_NAME and MESSAGE_NAME here
constname='spaces/SPACE_NAME/messages/MESSAGE_NAME';
constparameters={};
// Make the request
constresponse=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.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025年10月13日 UTC.