Mit Sammlungen den Überblick behalten Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
In diesem Leitfaden wird beschrieben, wie Sie die Methode delete() für die Ressource Space der Google Chat API verwenden, um einen benannten Bereich zu löschen, wenn er nicht mehr benötigt wird. Wenn Sie einen Gruppenbereich löschen, werden auch alle darin enthaltenen Elemente gelöscht, einschließlich Nachrichten und Anhänge.
Wenn Sie Google Workspace-Administrator sind, können Sie die Methode delete() aufrufen, um einen benannten Gruppenbereich in Ihrer Google Workspace-Organisation zu löschen.
Die Space-Ressource stellt einen Ort dar, an dem Personen und Chat-Apps Nachrichten senden, Dateien teilen und zusammenarbeiten können. Es gibt verschiedene Arten von Bereichen:
Direktnachrichten (DNs) sind Unterhaltungen zwischen zwei Nutzern oder zwischen einem Nutzer und einer Chat-App.
Gruppenchats sind Unterhaltungen zwischen drei oder mehr Nutzern und Chat-Apps.
Benannte Gruppenbereiche sind dauerhafte Orte, an denen Nutzer Nachrichten senden, Dateien teilen und zusammenarbeiten können.
import{createClientWithUserCredentials}from'./authentication-utils.js';constUSER_AUTH_OAUTH_SCOPES=['https://www.googleapis.com/auth/chat.delete'];// This sample shows how to delete a space with user credentialasyncfunctionmain(){// Create a clientconstchatClient=awaitcreateClientWithUserCredentials(USER_AUTH_OAUTH_SCOPES);// Initialize request argument(s)constrequest={// Replace SPACE_NAME herename:'spaces/SPACE_NAME'};// Make the requestconstresponse=awaitchatClient.deleteSpace(request);// Handle the responseconsole.log(response);}main().catch(console.error);
Ersetzen Sie SPACE_NAME durch die ID aus dem Feld name des Arbeitsbereichs, um dieses Beispiel auszuführen. Sie können die ID abrufen, indem Sie die Methode ListSpaces() aufrufen oder die URL des Bereichs verwenden.
Übergeben Sie die name des zu löschenden Gruppenbereichs.
Skript schreiben, das die Chat API aufruft
So löschen Sie einen Gruppenbereich:
Python
Erstellen Sie in Ihrem Arbeitsverzeichnis eine Datei mit dem Namen chat_space_delete_app.py.
Fügen Sie den folgenden Code in chat_space_delete_app.py ein:
fromgoogle.oauth2importservice_accountfromapiclient.discoveryimportbuild# 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"]defmain():''' 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)# 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()
Ersetzen Sie im Code Folgendes:
SPACE mit dem Namen des Bereichs. Sie können ihn mit der spaces.list-Methode in der Chat API oder aus der URL eines Bereichs abrufen.
Erstellen Sie das Beispiel in Ihrem Arbeitsverzeichnis und führen Sie es aus:
python3chat_space_delete_app.py
Wenn der Vorgang erfolgreich ist, ist der Antworttext leer. Das bedeutet, dass der Bereich gelöscht wurde.
Benannten Gruppenbereich als Google Workspace-Administrator löschen
Wenn Sie Google Workspace-Administrator sind, können Sie die Methode DeleteSpace() aufrufen, um einen benannten Gruppenbereich in Ihrer Google Workspace-Organisation zu löschen.
So rufen Sie diese Methode als Google Workspace-Administrator auf:
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Benötigte Informationen nicht gefunden","missingTheInformationINeed","thumb-down"],["Zu umständlich/zu viele Schritte","tooComplicatedTooManySteps","thumb-down"],["Nicht mehr aktuell","outOfDate","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Problem mit Beispielen/Code","samplesCodeIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],["Zuletzt aktualisiert: 2025-07-29 (UTC)."],[[["\u003cp\u003eThis guide explains how to delete a Google Chat space and its contents (messages, attachments) using the \u003ccode\u003edelete()\u003c/code\u003e method.\u003c/p\u003e\n"],["\u003cp\u003eGoogle Workspace administrators can delete any named space within their organization.\u003c/p\u003e\n"],["\u003cp\u003ePrerequisites include a Google Workspace account, a Google Cloud project, and necessary API configurations.\u003c/p\u003e\n"],["\u003cp\u003eTwo deletion methods are outlined: one using user authentication for personal spaces and another using app authentication (developer preview) for app-created spaces.\u003c/p\u003e\n"],["\u003cp\u003eGoogle Workspace administrators have the additional capability to delete any named space using admin privileges.\u003c/p\u003e\n"]]],["The guide details deleting named spaces in Google Chat via the `delete()` method. Users can delete spaces they have access to by specifying the `chat.delete` scope, calling `DeleteSpace()`, and providing the space's name. Chat apps can delete spaces they created using `chat.app.delete` scope and an API key. Google Workspace admins can delete any named space by calling `DeleteSpace()`, using appropriate authorization scopes with `useAdminAccess` set to `true` in the request. Deleting a space removes all its content.\n"],null,["This guide explains how use the\n[`delete()`](/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.ChatService.DeleteSpace)\nmethod on the `Space` resource of the Google Chat API to delete a named space when\nit's no longer needed. Deleting a space also deletes everything that it\ncontains, including messages and attachments.\n\nIf you're a Google Workspace administrator, you can call the `delete()`\nmethod to delete any named space in your Google Workspace organization.\n\nThe\n[`Space` resource](/workspace/chat/api/reference/rest/v1/spaces)\nrepresents a place where people and Chat apps can send messages,\nshare files, and collaborate. There are several types of spaces:\n\n- Direct messages (DMs) are conversations between two users or a user and a Chat app.\n- Group chats are conversations between three or more users and Chat apps.\n- Named spaces are persistent places where people send messages, share files, and collaborate.\n\nPrerequisites\n\n\nNode.js\n\n- A Business or Enterprise [Google Workspace](https://support.google.com/a/answer/6043576) account with access to [Google Chat](https://workspace.google.com/products/chat/).\n\n\u003c!-- --\u003e\n\n- Set up your environment:\n - [Create a Google Cloud project](/workspace/guides/create-project).\n - [Configure the OAuth consent screen](/workspace/guides/configure-oauth-consent).\n - [Enable and configure the Google Chat API](/workspace/chat/configure-chat-api) with a name, icon, and description for your Chat app.\n - Install the Node.js [Cloud Client Library](/workspace/chat/libraries?tab=nodejs#cloud-client-libraries).\n - [Create OAuth client ID credentials](/workspace/chat/authenticate-authorize-chat-user#step-2:) for a desktop application. To run the sample in this guide, save the credentials as a JSON file named `credentials.json` to your local directory.\n\n For guidance, complete the steps for setting up your environment in this [quickstart](/workspace/chat/api/guides/quickstart/nodejs\n #set-up-environment).\n- [Choose an authorization scope](/workspace/chat/authenticate-authorize#asynchronous-chat-calls) that supports user authentication.\n\n\u003c!-- --\u003e\n\n- A Google Chat space. To create one using the Google Chat API, see [Create a space](/workspace/chat/create-spaces). To create one in Chat, visit the [Help Center documentation](https://support.google.com/chat/answer/12176488).\n\n\n| The code samples in this page use the gRPC API interface with the Google Cloud client libraries. Alternatively, you can use the REST API interface. For more information about the gRPC and REST interfaces, see [Google Chat API overview](/workspace/chat/api/reference).\n\n\u003cbr /\u003e\n\nDelete a named space as a user\n\nTo delete an existing space in Google Chat with\n[user authentication](/workspace/chat/authenticate-authorize-chat-user), pass\nthe following in your request:\n\n- Specify the `chat.delete` authorization scope.\n- Call the [`DeleteSpace()`](/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.ChatService.DeleteSpace) method.\n- Pass the `name` of the space to delete.\n\nHere's how to delete a space: \n\nNode.js \nchat/client-libraries/cloud/delete-space-user-cred.js \n[View on GitHub](https://github.com/googleworkspace/node-samples/blob/main/chat/client-libraries/cloud/delete-space-user-cred.js) \n\n```javascript\nimport {createClientWithUserCredentials} from './authentication-utils.js';\n\nconst USER_AUTH_OAUTH_SCOPES = ['https://www.googleapis.com/auth/chat.delete'];\n\n// This sample shows how to delete a space with user credential\nasync function main() {\n // Create a client\n const chatClient = await createClientWithUserCredentials(USER_AUTH_OAUTH_SCOPES);\n\n // Initialize request argument(s)\n const request = {\n // Replace SPACE_NAME here\n name: 'spaces/SPACE_NAME'\n };\n\n // Make the request\n const response = await chatClient.deleteSpace(request);\n\n // Handle the response\n console.log(response);\n}\n\nmain().catch(console.error);\n```\n\nTo run this sample, replace \u003cvar translate=\"no\"\u003eSPACE_NAME\u003c/var\u003e with the ID from\nthe space's\n[`name`](/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.Space.FIELDS.string.google.chat.v1.Space.name)\nfield. You can obtain the ID by calling the\n[`ListSpaces()`](/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.ChatService.ListSpaces)\nmethod or from the space's URL.\n\nDelete a named space as a Chat app **Developer Preview:** Available as part of the [Google Workspace Developer Preview Program](https://developers.google.com/workspace/preview), which grants early access to certain features.\n\nApp authentication requires one-time\n[administrator approval](/workspace/chat/authenticate-authorize-chat-app#admin-approval).\n\nWith app authentication, you can only delete spaces created by\nChat apps.\n\nTo delete an existing space in Google Chat with\n[app authentication](/workspace/chat/authenticate-authorize-chat-app), pass\nthe following in your request:\n\n- Specify the `chat.app.delete` authorization scope.\n- Call the [`delete` method](/workspace/chat/api/reference/rest/v1/spaces/delete) on the [`Space` resource](/workspace/chat/api/reference/rest/v1/spaces).\n- Pass the `name` of the space to delete.\n\nCreate an API key\n\n\nTo 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.\n\n\nTo create the API Key, open your app's Google Cloud project and do the following:\n\n1. In the Google Cloud console, go to Menu menu \\\u003e **APIs \\& Services** \\\u003e **Credentials** .\n\n [Go to Credentials](https://console.cloud.google.com/apis/credentials)\n2. Click **Create credentials** \\\u003e **API key**.\n3. Your new API key is displayed.\n - Click Copy content_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.\n - Click **Restrict key** to update advanced settings and limit use of your API key. For more details, see [Applying API key restrictions](https://cloud.google.com/docs/authentication/api-keys#api_key_restrictions).\n\nWrite a script that calls Chat API\n\nHere's how to delete a space: \n\nPython\n\n1. In your working directory, create a file named `chat_space_delete_app.py`.\n2. Include the following code in `chat_space_delete_app.py`:\n\n from google.oauth2 import service_account\n from apiclient.discovery import build\n\n # Define your app's authorization scopes.\n # When modifying these scopes, delete the file token.json, if it exists.\n SCOPES = [\"https://www.googleapis.com/auth/chat.app.delete\"]\n\n def main():\n '''\n Authenticates with Chat API using app authentication,\n then deletes the specified space.\n '''\n\n # Specify service account details.\n creds = (\n service_account.Credentials.from_service_account_file('credentials.json')\n .with_scopes(SCOPES)\n )\n\n # Build a service endpoint for Chat API.\n chat = build('chat', 'v1', credentials=creds, discoveryServiceUrl='https://chat.googleapis.com/$discovery/rest?version=v1&labels=DEVELOPER_PREVIEW&key=\u003cvar translate=\"no\"\u003eAPI_KEY\u003c/var\u003e')\n\n # Use the service endpoint to call Chat API.\n result = chat.spaces().delete(\n\n # The space to delete.\n #\n # Replace SPACE with a space name.\n # Obtain the space name from the spaces resource of Chat API,\n # or from a space's URL.\n name='spaces/\u003cvar translate=\"no\"\u003eSPACE\u003c/var\u003e'\n\n ).execute()\n\n # Print Chat API's response in your command line interface.\n # When deleting a space, the response body is empty.\n print(result)\n\n if __name__ == '__main__':\n main()\n\n3. In the code, replace the following:\n\n - \u003cvar translate=\"no\"\u003eAPI_KEY\u003c/var\u003e: the API key you created to build\n the service endpoint for Chat API.\n\n - \u003cvar translate=\"no\"\u003eSPACE\u003c/var\u003e with the space name,\n which you can obtain from the\n [`spaces.list` method](/workspace/chat/api/reference/rest/v1/spaces/list)\n in the Chat API, or from a space's URL.\n\n4. In your working directory, build and run the sample:\n\n python3 chat_space_delete_app.py\n\nIf successful, the response body is empty, which indicates that the space is\ndeleted.\n\nDelete a named space as a Google Workspace administrator\n\nIf you're a Google Workspace administrator, you can call the\n`DeleteSpace()` method to delete any named space in your\nGoogle Workspace organization.\n\nTo call this method as a Google Workspace administrator, do the following:\n\n- Call the method using user authentication, and specify an [authorization scope](/workspace/chat/authenticate-authorize#asynchronous-chat-calls) that supports calling the method using [administrator privileges](/workspace/chat/authenticate-authorize-chat-user#admin-privileges).\n- In your request, specify the query parameter `useAdminAccess` to `true`.\n\nFor more information and examples, see\n[Manage Google Chat spaces as a Google Workspace administrator](/workspace/chat/admin-overview).\n\nRelated topics\n\n- [Create a space](/workspace/chat/create-spaces)\n- [Get details about a space](/workspace/chat/get-spaces).\n- [List spaces](/workspace/chat/list-spaces).\n- [Update a space](/workspace/chat/update-spaces).\n- [Delete a space](/workspace/chat/delete-spaces).\n- [Set up a space](/workspace/chat/set-up-spaces).\n- [Find a direct message space](/workspace/chat/find-direct-message-in-spaces)."]]