Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Hướng dẫn này giải thích cách sử dụng phương thức delete() trên tài nguyên Space của Google Chat API để xoá một không gian có tên khi không còn cần thiết. Khi xoá một không gian, mọi nội dung trong không gian đó cũng sẽ bị xoá, bao gồm cả tin nhắn và tệp đính kèm.
Nếu là quản trị viên Google Workspace, bạn có thể gọi phương thức delete()để xoá mọi không gian có tên trong tổ chức Google Workspace của mình.
Tài nguyên Space đại diện cho một nơi mà mọi người và các ứng dụng Chat có thể gửi tin nhắn, chia sẻ tệp và cộng tác. Có một số loại không gian như sau:
Tin nhắn trực tiếp (DM) là cuộc trò chuyện giữa hai người dùng hoặc giữa một người dùng và một ứng dụng Chat.
Cuộc trò chuyện nhóm là cuộc trò chuyện giữa 3 người dùng trở lên và các ứng dụng Chat.
Không gian có tên là những nơi ổn định để mọi người gửi tin nhắn, chia sẻ tệp và cộng tác.
Tạo thông tin đăng nhập mã ứng dụng khách OAuth cho một ứng dụng dành cho máy tính. Để chạy mẫu trong hướng dẫn này, hãy lưu thông tin đăng nhập dưới dạng tệp JSON có tên là credentials.json vào thư mục cục bộ của bạn.
Để được hướng dẫn, hãy hoàn tất các bước thiết lập môi trường trong phần hướng dẫn nhanh này.
Một không gian trong Google Chat. Để tạo một không gian bằng Google Chat API, hãy xem phần Tạo không gian. Để tạo một tài liệu trong Chat, hãy truy cập vào tài liệu trong Trung tâm trợ giúp.
Xoá không gian có tên với tư cách là người dùng
Để xoá một không gian hiện có trong Google Chat bằng xác thực người dùng, hãy truyền các thông tin sau trong yêu cầu của bạn:
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);
Để chạy mẫu này, hãy thay thế SPACE_NAME bằng mã nhận dạng trong trường name của không gian. Bạn có thể lấy mã nhận dạng bằng cách gọi phương thức ListSpaces() hoặc từ URL của không gian.
Trong thư mục làm việc, hãy tạo một tệp có tên là chat_space_delete_app.py.
Thêm mã sau vào chat_space_delete_app.py:
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()
Trong mã, hãy thay thế những nội dung sau:
SPACE cùng với tên không gian mà bạn có thể lấy từ phương thức spaces.list trong API Chat hoặc từ URL của một không gian.
Trong thư mục làm việc, hãy tạo và chạy mẫu:
python3chat_space_delete_app.py
Nếu thành công, nội dung phản hồi sẽ trống, cho biết không gian đã bị xoá.
Xoá không gian có tên với tư cách là quản trị viên Google Workspace
Nếu là quản trị viên Google Workspace, bạn có thể gọi phương thức DeleteSpace() để xoá mọi không gian có tên trong tổ chức Google Workspace của mình.
Để gọi phương thức này với tư cách là quản trị viên Google Workspace, hãy làm như sau:
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-08-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,["# Delete a space\n\nThis 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\n\n### Node.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------------------------------\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\n### Node.js\n\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\n----------------------------------\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\n### Write a script that calls Chat API\n\nHere's how to delete a space: \n\n### Python\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)\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\"\u003eSPACE\u003c/var\u003e with the space name, which you can obtain from the [`spaces.list` method](/workspace/chat/api/reference/rest/v1/spaces/list) in the Chat API, or from a space's URL.\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--------------------------------------------------------\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\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)."]]