Delete a message
Stay organized with collections
Save and categorize content based on your preferences.
AI-generated Key Takeaways
-
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 thename
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
- A Business or Enterprise Google Workspace account with access to Google Chat.
- Set up your environment:
- Create a Google Cloud project.
- Configure the OAuth consent screen.
- Enable and configure the Google Chat API with a name, icon, and description for your Chat app.
- Install the Node.js Cloud Client Library.
- Create access credentials based on how you want to authenticate in your Google Chat API
request:
- To authenticate as a Chat user,
create OAuth client ID
credentials and save the credentials as a JSON file named
credentials.json
to your local directory. - To authenticate as the Chat app,
create service account
credentials and save the credentials as a JSON file named
credentials.json
.
- To authenticate as a Chat user,
create OAuth client ID
credentials and save the credentials as a JSON file named
- Choose an authorization scope based on whether you want to authenticate as a user or the Chat app.
Python
- A Business or Enterprise Google Workspace account with access to Google Chat.
- Set up your environment:
- Create a Google Cloud project.
- Configure the OAuth consent screen.
- Enable and configure the Google Chat API with a name, icon, and description for your Chat app.
- Install the Python Cloud Client Library.
- Create access credentials based on how you want to authenticate in your Google Chat API
request:
- To authenticate as a Chat user,
create OAuth client ID
credentials and save the credentials as a JSON file named
credentials.json
to your local directory. - To authenticate as the Chat app,
create service account
credentials and save the credentials as a JSON file named
credentials.json
.
- To authenticate as a Chat user,
create OAuth client ID
credentials and save the credentials as a JSON file named
- Choose an authorization scope based on whether you want to authenticate as a user or the Chat app.
Java
- A Business or Enterprise Google Workspace account with access to Google Chat.
- Set up your environment:
- Create a Google Cloud project.
- Configure the OAuth consent screen.
- Enable and configure the Google Chat API with a name, icon, and description for your Chat app.
- Install the Java Cloud Client Library.
- Create access credentials based on how you want to authenticate in your Google Chat API
request:
- To authenticate as a Chat user,
create OAuth client ID
credentials and save the credentials as a JSON file named
credentials.json
to your local directory. - To authenticate as the Chat app,
create service account
credentials and save the credentials as a JSON file named
credentials.json
.
- To authenticate as a Chat user,
create OAuth client ID
credentials and save the credentials as a JSON file named
- Choose an authorization scope based on whether you want to authenticate as a user or the Chat app.
Apps Script
- A Business or Enterprise Google Workspace account with access to Google Chat.
- Set up your environment:
- Create a Google Cloud project.
- Configure the OAuth consent screen.
- Enable and configure the Google Chat API with a name, icon, and description for your Chat app.
- Create a standalone Apps Script project, and turn on the Advanced Chat Service.
- In this guide, you must use either user or app authentication. To authenticate as the Chat app, create service account credentials. For steps, see Authenticate and authorize as a Google Chat app.
- Choose an authorization scope based on whether you want to authenticate as a user or the Chat app.
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
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
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
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
/** * 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'sname
. You can obtain the ID by calling theListSpaces()
method or from the space's URL.MESSAGE_NAME
: the ID from the message'sname
. 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
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
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
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
/** * 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'sname
. You can obtain the ID by calling theListSpaces()
method or from the space's URL.MESSAGE_NAME
: the ID from the message'sname
. 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.
Related topics
- Format a message.
- Get details about a message.
- List messages in a space.
- Update a message.
- Send a message.