LoginSignup
0
0

More than 3 years have passed since last update.

Node.jsでGoogle Drive上のファイルを削除する (Google Drive API v3)

Posted at

Google Drive APIをNode.jsから触る以下の記事たちの続きです

メソッドはdeleteを使います。

今までやった中で一番シンプルかも

スコープ

スコープは以下の三つなのでリネームなどで利用したものを再利用できますね

https://www.googleapis.com/auth/drive
https://www.googleapis.com/auth/drive.file
https://www.googleapis.com/auth/drive.appdata

基本はリネーム時のdrive.files.update()のコードで

チュートリアルコードのlistFiles関数をmain関数として定義しなおして以下のように呼び出します。

rename.js
//省略

function main(auth) {
    const targetFileId = `xxxxxxxxxxxxxxxxxxxx`; //削除したいファイルID

    const drive = google.drive({version: 'v3', auth});
    const params = {
        fileId: targetFileId,
    };

    drive.files.delete(params)
    .then(res => console.log(res))
    .catch(err => console.log(err));
}

所感

他ので慣れてきたのでこれはかなり簡単。
前回のコピーと移動のあたりで複数参照しているファイルを消すと複数の参照元から消えるので注意。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0