Google Drive: Delete File / Folder

Google Drive: Delete File / Folder

Google ドライブ: ファイル / フォルダ削除

This item deletes files or folders on Google Drive.

Auto Step icon
Basic Configs
Step Name
Note
Configs for this Auto Step
UserID
C1: User connects to Google Drive (must be App Administrator) *
FileIdsItem
C2: File / Folder IDs to delete (Write one per line) *

Notes

  • Users in C1 need to have a configured connection with Google Drive in [Account Settings] > [Google Connectivity]
    • Google Workspace Connectivity ([System Settings] > [Google Connectivity]) must be enabled on the workflow platform ([System Administrator Authorization] required )
  • You can delete files and folders created by “Google Drive: File Upload”, “Google Drive: Create Folder”, and “Google Sheets: Create File”.
  • Deleted Files / Folders are sent to Trash.
  • Empty rows in the Data Item in C2 are ignored

Capture

See also

Script (click to open)
  • An XML file that contains the code below is available to download
    • google-drive-file-delete.xml (C) Questetra, Inc. (MIT License)
    • If you are using Professional, you can modify the contents of this file and use it as your own add-on auto step


function main() {
    const filesIdArray = getFileIds();
    const quser = getQuser();
    for (let i = 0; i < filesIdArray.length; i++) {
        deleteFile(quser, filesIdArray[i]);
    }
}

/**
 * データ項目からファイル / フォルダ ID を取得し、配列に入れて返す
 * ID の数が通信制限を超えていればエラー
 * @return {Array<String>}  ファイル ID の配列
 */
function getFileIds() {
    const fileIds = engine.findDataByNumber(configs.get("FileIdsItem"));
    if (fileIds === null) {
        throw "File / Folder IDs aren't set.";
    }
    let fileIdsArray = fileIds.split("\n");
    fileIdsArray = fileIdsArray.filter(lines => lines !== ""); // 空文字列を削除
    if (fileIdsArray.length === 0) {
        throw "File / Folder IDs aren't set.";
    }
    if (fileIdsArray.length > httpClient.getRequestingLimit()) {
        //check number of files
        throw "Number of File IDs is over the limit.";
    }
    return fileIdsArray;
}

/**
 * 実行ユーザを取得する
 * @return {QuserView} 実行ユーザ
 */
function getQuser() {
    const quser = configs.getObject("UserID");
    if (quser === null) {
        throw "User not found.";
    }
    engine.log(`User Name: ${quser.getName()}`);
    return quser;
}

/**
 * ファイルを削除する
 * メタデータの "trashed" パラメータを true にすることでファイルをゴミ箱に入れる
 * 削除の結果をログ出力し、失敗した場合はエラーとする
 * @param {QuserView} quser  実行ユーザ
 * @param {String} fileId  削除するファイルの ID
 */
function deleteFile(quser, fileId) {
    const url = `https://www.googleapis.com/drive/v3/files/${fileId}`;
    const body = {
        trashed: true
    };
    let response = httpClient.begin()
        .googleOAuth2(quser, "Drive")
        .queryParam("supportsAllDrives", "true")
        .body(JSON.stringify(body), "application/json; charset=UTF-8")
        .patch(url);

    const status = response.getStatusCode();
    if (status >= 300) {
        engine.log(response.getResponseAsString());
        throw `Failed to delete. ID: ${fileId}, status: ${status}`;
    } else {
        engine.log(`Succeed to delete. ID: ${fileId}`);
    }
}

1 thought on “Google Drive: Delete File / Folder”

  1. Pingback: Utilizing Google Drive from Workflow – Sending files to people outside the company – Questetra Support

Comments are closed.

Discover more from Questetra Support

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top