首页 > 解决方案 > 删除多个无组织/孤立的谷歌驱动器文件

问题描述

我想删除 Google Drive 文件夹中的数千个文件。我删除了该文件夹,社区中的许多人都知道(但我不知道)使文件存在但孤立。我可以通过“is:unorganized owner:me”列出文件,但一次不能选择超过几百个来(慢慢地)删除。

是否有一个脚本可以搜索和删除这些文件,并且只有这些文件?谢谢

标签: google-apps-scriptgoogle-drive-api

解决方案


我相信你的目标如下。

  • 您想要检索搜索过的文件is:unorganized owner:me并想要删除这些文件。
  • 您想降低此流程的流程成本。

问题和解决方法:

遗憾的是,在当前阶段,无法通过is:unorganized owner:meDrive API 和 Drive 服务直接检索文件。因此,作为当前的解决方法,我想建议以下流程。

  1. 检索 Google Drive 中所有文件的文件列表。在这种情况下,'me' in owners and trashed = false用作搜索查询。为此使用 Drive API 的“文件:列表”方法。
  2. 使用脚本检索没有父文件夹的文件的文件列表。
    • 通过上述流程1和2,is:unorganized owner:me可以使用脚本来实现搜索。
  3. 使用 Drive API 的“文件:更新”方法,使用检索到的文件列表将文件移动到垃圾箱。
    • 在这种情况下,不会直接删除文件。因为直接删除文件的时候,我觉得这种情况很危险。当然,也可以使用Drive API的“文件:删除”的方法直接删除文件。参考
    • 根据您的问题,我认为您可能要删除很多文件。所以在这个答案中,我想建议使用批处理请求。

当上述流程反映到脚本时,它变成如下。

重要提示:请小心!

  • 当您使用此示例脚本时,请注意这一点。我想推荐以下使用此脚本的流程。

    1. 检索文件列表,is:unorganized owner:me检查文件列表中的所有文件是否都是您要删除的文件。
      • const fileList = getFileList(token);您可以使用inmain()函数检索文件列表。
    2. 当您可以完全确认文件列表中的所有文件都是您要删除的文件时,请使用const res = moveFilesToTrashBox(token, fileList);. 这样,文件列表中的文件就会被移动到垃圾箱。
    3. 请确认垃圾箱。当包含不想删除的文件时,请恢复它们。

示例脚本:

在使用此脚本之前,请在 Advanced Google services 中启用 Drive API。请运行main()功能。

// Move the files in the file list to the trash box.
function moveFilesToTrashBox(token, fileList) {
  const limit = 100;
  const split = Math.ceil(fileList.length / limit);
  const res = [];
  for (let i = 0; i < split; i++) {
    const boundary = "xxxxxxxxxx";
    const payload = fileList.splice(0, limit).reduce((s, {id}, i, a) => s += "Content-Type: application/http\r\n" +
    "Content-ID: " + i + "\r\n\r\n" +
    "PATCH https://www.googleapis.com/drive/v3/files/" + id + "\r\n" +
    "Content-Type: application/json\r\n\r\n" +
    JSON.stringify({trashed: true}) + "\r\n" +
    "--" + boundary + (i == a.length - 1 ? "--" : "") + "\r\n" , "--" + boundary + "\r\n");
    const params = {
      method: "post",
      contentType: "multipart/mixed; boundary=" + boundary,
      payload: payload,
      headers: {Authorization: "Bearer " + token},
      muteHttpExceptions: true,
    };
    const r = UrlFetchApp.fetch("https://www.googleapis.com/batch/drive/v3", params);
    res.push(r.getContentText());
  }
  return res;
}

// Retrieve the file list by searching with "is:unorganized owner:me".
function getFileList(token) {
  const fields = decodeURIComponent("nextPageToken,files(name,id,mimeType,parents)");
  const q = decodeURIComponent("'me' in owners and trashed = false");
  let allFiles = [];
  let pageToken = "";
  do {
    const res = UrlFetchApp.fetch(
      `https://www.googleapis.com/drive/v3/files?pageSize=1000&fields=${fields}&q=${q}&pageToken=${pageToken}`,
      { headers: { authorization: `Bearer ${token}` } }
    );
    const obj = JSON.parse(res);
    allFiles = allFiles.concat(obj.files);
    pageToken = obj.nextPageToken;
  } while (pageToken);
  return allFiles.filter(({ parents }) => !parents);  
}

// Please run this function.
function main() {
  const token = ScriptApp.getOAuthToken();
  
  // Retrieve the file list of all files in the Google Drive.
  const fileList = getFileList(token);
  console.log(fileList.length);
  console.log(fileList);
  
  // Move the files to the trash box using the retrieved file list.
  // When you could completely confirm that all files of the file list are the files you want to delete, please use the below script.
  // const res = moveFilesToTrashBox(token, fileList);
  // console.log(res);


  // DriveApp.createFile(); // This is used for automatically adding a scope of "https://www.googleapis.com/auth/drive".
}

参考:


推荐阅读