This guide explains how use the
delete()
method on the Space
resource of the Google Chat API to delete a named space when
it's no longer needed. Deleting a space also deletes everything that it
contains, including messages and attachments.
If you're a Google Workspace administrator, you can call the delete()
method to delete any named space in your Google Workspace organization.
The
Space
resource
represents a place where people and Chat apps can send messages,
share files, and collaborate. There are several types of spaces:
- Direct messages (DMs) are conversations between two users or a user and a Chat app.
- Group chats are conversations between three or more users and Chat apps.
- Named spaces are persistent places where people send messages, share files, and collaborate.
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 OAuth client ID credentials for a desktop application. To run the sample in this
guide, save the credentials as a JSON file named
client_secrets.json
to your local directory.
- Choose an authorization scope that supports user authentication.
- A Google Chat space. To create one using the Google Chat API, see Create a space. To create one in Chat, visit the Help Center documentation.
Delete a named space as a user
To delete an existing space in Google Chat with user authentication, pass the following in your request:
- Specify the
chat.delete
authorization scope. - Call the
DeleteSpace()
method. - Pass the
name
of the space to delete.
Here's how to delete a space:
Node.js
To run this sample, replace SPACE_NAME
with the ID from
the space's
name
field. You can obtain the ID by calling the
ListSpaces()
method or from the space's URL.
Delete a named space as a Chat app
App authentication requires one-time administrator approval.
With app authentication, you can only delete spaces created by Chat apps.
To delete an existing space in Google Chat with app authentication, pass the following in your request:
- Specify the
chat.app.delete
authorization scope. - Call the
delete
method on theSpace
resource. - Pass the
name
of the space to delete.
Create an API key
To call a Developer Preview API method, you must use a non-public developer preview version of the API discovery document. To authenticate the request, you must pass an API key.
To create the API Key, open your app's Google Cloud project and do the following:
- In the Google Cloud console, go to Menu > APIs & Services > Credentials.
- Click Create credentials > API key.
- Your new API key is displayed.
- Click Copy to copy your API key for use in your app's code. The API key can also be found in the "API keys" section of your project's credentials.
- Click Restrict key to update advanced settings and limit use of your API key. For more details, see Applying API key restrictions.
Write a script that calls Chat API
Here's how to delete a space:
Python
- In your working directory, create a file named
chat_space_delete_app.py
. Include the following code in
chat_space_delete_app.py
:from google.oauth2 import service_account from apiclient.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.app.delete"] def main(): ''' Authenticates with Chat API using app authentication, then deletes the specified space. ''' # Specify service account details. creds = ( service_account.Credentials.from_service_account_file('credentials.json') .with_scopes(SCOPES) ) # Build a service endpoint for Chat API. chat = build('chat', 'v1', credentials=creds, discoveryServiceUrl='https://chat.googleapis.com/$discovery/rest?version=v1&labels=DEVELOPER_PREVIEW&key=API_KEY') # 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()
In the code, replace the following:
API_KEY
: the API key you created to build the service endpoint for Chat API.SPACE
with the space name, which you can obtain from thespaces.list
method in the Chat API, or from a space's URL.
In your working directory, build and run the sample:
python3 chat_space_delete_app.py
If successful, the response body is empty, which indicates that the space is deleted.
Delete a named space as a Google Workspace administrator
If you're a Google Workspace administrator, you can call the
DeleteSpace()
method to delete any named space in your
Google Workspace organization.
To call this method as a Google Workspace administrator, do the following:
- Call the method using user authentication, and specify an authorization scope that supports calling the method using administrator privileges.
- In your request, specify the query parameter
useAdminAccess
totrue
.
For more information and examples, see Manage Google Chat spaces as a Google Workspace administrator.
Related topics
- Create a space
- Get details about a space.
- List spaces.
- Update a space.
- Delete a space.
- Set up a space.
- Find a direct message space.