移除文件的标签
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
本页介绍了如何移除单个 Google 云端硬盘文件上的标签。
如需从文件中移除文件标签元数据,请使用 files.modifyLabels
方法。请求正文包含 ModifyLabelsRequest
的一个实例,用于修改文件上的一组标签。该请求可能包含以原子方式应用的多项修改。也就是说,如果任何修改无效,则整个更新都会失败,并且不会应用任何(可能相关的)更改。
ModifyLabelsRequest
包含 LabelModification
的实例,该实例是对文件标签的修改。它还可能包含 FieldModification
的实例,该实例是对标签字段的修改。如需从文件中移除标签,请将 FieldModification.removeLabel
设置为 True
。
如果成功,响应正文将包含请求添加或更新的标签。这些对象存在于 Label
类型的 modifiedLabels
对象中。
示例
以下代码示例展示了如何使用 labelId
通过 fileId
移除与标签关联的所有字段。例如,如果某个标签同时包含文本和用户字段,那么移除该标签会删除与该标签关联的文本和用户字段。而取消设置文本字段会将其从标签中移除,但用户字段保持不变。如需了解详情,请参阅取消设置文件中的标签字段。
Java
ModifyLabelsRequest modifyLabelsRequest =
new ModifyLabelsRequest()
.setLabelModifications(
ImmutableList.of(
new LabelModification()
.setLabelId("LABEL_ID")
.setRemoveLabel(true)));
ModifyLabelsResponse modifyLabelsResponse = driveService.files().modifyLabels("FILE_ID", modifyLabels
Request).execute();
Python
label_modification = {'labelId':'LABEL_ID', 'removeLabel': True]}
modified_labels = drive_service.files().modifyLabels(fileId="FILE_ID", body = {'labelModifications
' : [label_modification]}).execute();
Node.js
/**
* Remove a label on a Drive file
* @return{obj} updated label data
**/
async function removeLabel() {
// Get credentials and build service
// TODO (developer) - Use appropriate auth mechanism for your app
const {GoogleAuth} = require('google-auth-library');
const {google} = require('googleapis');
const auth = new GoogleAuth({scopes: 'https://www.googleapis.com/auth/drive'});
const service = google.drive({version: 'v3', auth});
const labelModification = {
'labelId': 'LABEL_ID',
'removeLabel': True,
};
const labelModificationRequest = {
'labelModifications': [labelModification],
};
try {
const updateResponse = await service.files.modifyLabels({
fileId: 'FILE_ID',
resource: labelModificationRequest,
});
return updateResponse;
}
catch (err) {
// TODO (developer) - Handle error
throw err;
}
替换以下内容:
- LABEL_ID:要修改的标签的
labelId
。如需查找文件上的标签,请使用 files.listLabels
方法。
- FILE_ID:标签所修改的文件的
fileId
。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-09-01。
[null,null,["最后更新时间 (UTC):2025-09-01。"],[],[],null,["This page describes how to remove a label on a single Google Drive file.\n\nTo remove the file label metadata from a file, use the\n[`files.modifyLabels`](/workspace/drive/api/v2/reference/files/modifyLabels) method. The\n[request body](/workspace/drive/api/reference/rest/v2/files/modifyLabels#request-body)\ncontains an instance of\n[`ModifyLabelsRequest`](/workspace/drive/api/reference/rest/v2/files/modifyLabels#modifylabelsrequest)\nto modify the set of labels on a file. The request might contain several\nmodifications that are applied atomically. That is, if any modifications aren't\nvalid, then the entire update is unsuccessful and none of the (potentially\ndependent) changes are applied.\n\nThe `ModifyLabelsRequest` contains an instance of\n[`LabelModification`](/workspace/drive/api/reference/rest/v2/files/modifyLabels#labelmodification)\nwhich is a modification to a label on a file. It might also contain an instance\nof\n[`FieldModification`](/workspace/drive/api/reference/rest/v2/files/modifyLabels#fieldmodification)\nwhich is a modification to a label's field. To remove the label from the file,\nset `FieldModification.removeLabel` to `True`.\n\nIf successful, the [response\nbody](/workspace/drive/api/reference/rest/v2/files/modifyLabels#response-body) contains\nthe labels added or updated by the request. These exist within a\n`modifiedLabels` object of type [`Label`](/workspace/drive/api/reference/rest/v2/Label).\n\nExample\n\nThe following code sample shows how to use the `labelId` to remove all fields\nassociated with the label using the `fileId`. For example, if a label contains\nboth text and user fields, removing a label deletes *both* the text and user\nfields associated with the label. Whereas, unsetting the text field removes it\nfrom the label but leaves the user field untouched. For more information, see\n[Unset a label field on a file](/workspace/drive/api/guides/unset-label). \n\nJava \n\n ModifyLabelsRequest modifyLabelsRequest =\n new ModifyLabelsRequest()\n .setLabelModifications(\n ImmutableList.of(\n new LabelModification()\n .setLabelId(&\u003cvar translate=\"no\"\u003equot;LAB\u003c/var\u003eEL_ID\")\n .setRemoveLabel(true)));\n\n ModifyLabelsResponse modifyLabelsResponse = driveService.files().modi\u003cvar translate=\"no\"\u003efyLabel\u003c/var\u003es(\"FILE_ID\", modifyLabelsRequest).execute();\n\nPython \n\n label_modification = {'labelI\u003cvar translate=\"no\"\u003ed'\u003c/var\u003e:'LABEL_ID', 'removeLabel': True]}\n\n modified_labels = drive_service.files\u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-p\"\u003e()\u003c/span\u003e\u003cspan class=\"devsite-syntax-o\"\u003e.\u003c/span\u003e\u003cspan class=\"devsite-syntax-n\"\u003emodi\u003c/span\u003e\u003c/var\u003efyLabels(fileId=\"FILE_ID\", body = {'labelModifications' : [label_modification]}).execute();\n\nNode.js \n\n /**\n * Remove a label on a Drive file\n * @return{obj} updated label data\n **/\n async function removeLabel() {\n // Get credentials and build service\n // TODO (developer) - Use appropriate auth mechanism for your app\n\n const {GoogleAuth} = require('google-auth-library');\n const {google} = require('googleapis');\n\n const auth = new GoogleAuth({scopes: 'https://www.googleapis.com/auth/drive'});\n const service = google.drive({version: 'v3', auth});\n const labelM\u003cvar translate=\"no\"\u003eodificat\u003c/var\u003eion = {\n 'labelId': 'LABEL_ID',\n 'removeLabel': True,\n };\n const labelModificationRequest = {\n 'labelModifications': [labelModification],\n };\n try {\n const upd\u003cvar translate=\"no\"\u003eateResp\u003c/var\u003eonse = await service.files.modifyLabels({\n fileId: 'FILE_ID',\n resource: labelModificationRequest,\n });\n return updateResponse;\n } catch (err) {\n // TODO (developer) - Handle error\n throw err;\n }\n\nReplace the following:\n\n- \u003cvar translate=\"no\"\u003eLABEL_ID\u003c/var\u003e: The `labelId` of the label to modify. To locate the labels on a file, use the [`files.listLabels`](/workspace/drive/api/v2/reference/files/listLabels) method.\n- \u003cvar translate=\"no\"\u003eFILE_ID\u003c/var\u003e: The `fileId` of the file for which the labels are modified."]]