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.
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
Python
- A Business or Enterprise Google Workspace account with access to Google Chat.
- 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
- 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 OAuth client ID credentials for a desktop application. To run the sample in this
guide, save the credentials as a JSON file (
client_secrets.json
) to your local directory.
- Choose an authorization scope that supports user authentication.
Node.js
- A Business or Enterprise Google Workspace account with access to Google Chat.
- Node.js 14 or greater
- The npm package management tool
- The latest Google client libraries. To install or update them,
run the following command in your command-line interface:
npm install @google-cloud/local-auth @googleapis/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 OAuth client ID credentials for a desktop application. To run the sample in this
guide, save the credentials as a JSON file (
client_secrets.json
) to your local directory.
- Choose an authorization scope that supports user authentication.
Delete a named space
To delete an existing space in Google Chat, pass the following in your request:
- Specify the
chat.delete
authorization scope. - Call the
delete
method on theSpace
resource. - Pass the
name
of the space to delete.
Here's how to delete a space:
Python
- In your working directory, create a file named
chat_space_delete.py
. Include the following code in
chat_space_delete.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.delete"] def main(): ''' Authenticates with Chat API via user credentials, then deletes the specified space. ''' # 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().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
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.py
Node.js
- In your working directory, create a file named
delete-space.js
. Include the following code in
delete-space.js
:const chat = require('@googleapis/chat'); const {authenticate} = require('@google-cloud/local-auth'); /** * Deletes a Chat space. * @return {!Promise<!Object>} */ async function deleteSpace() { const scopes = [ 'https://www.googleapis.com/auth/chat.delete', ]; const authClient = await authenticate({scopes, keyfilePath: 'client_secrets.json'}); const chatClient = await chat.chat({version: 'v1', auth: authClient}); return await chatClient.spaces.delete({name: 'spaces/SPACE'}); } deleteSpace().then(console.log);
In the code, replace
SPACE
with a space name, which you can obtain from thespaces.list
method in the Chat API, or from a space's URL.In your working directory, run the sample:
node delete-space.js
If successful, the response body is empty, which indicates that the space is deleted.
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.