0

I am trying to delete multiple files at once from my google drive using the REST API. I found this SO Question-bulk deletion Google Drive API, I have tried this solution without the npm package using fetch.

This is what I have at the moment.

                const boundary = 'END_OF_PART';
                const separation = `\n--'${boundary}'\n`;
                const ending = `\n--'${boundary}'--`;

                const requestBody = files.reduce((accum, current) => {
                    accum += separation +
                        'Content-Type: application/http\n\n' +
                        `DELETE https://www.googleapis.com/drive/v2/files/${current.id} 
                         \nAuthorization: Bearer ${this.getToken()}`
                    return accum
                }, '') + ending


                const _multiPart = [];
                files.forEach((file) => {
                    const obj = {
                        "Content-Type": "application/http",
                        body: `DELETE https://www.googleapis.com/drive/v2/files/${file.id}\n`,
                    };
                    _multiPart.push(obj);
                });

               const url = new URL('https://www.googleapis.com/batch/drive/v3');
                const requestOptions = {
                    method: 'POST',
                    headers: {
                        "Content-Type": "multipart/mixed",
                        'Authorization' : `Bearer ${this.getToken()}`
                    },
                    body: requestBody,
                    multipart: _multiPart,
                };
                await fetch(url, requestOptions);

When I run the deletion I get the id's of the files but the end result is Error 400 bad request. Documentation is scarce for this process. What is it that I am doing wrong here and how can I fix it to get this to work?

End result that I am looking for is deleting a large amount of files from my google drive.

Can anyone point me in the right direction on this ?.

EDIT: I just tried a solution at this SO Question: Bulk delete files on Google Drive with raw XMLHttpRequest

And it did delete 100 files, but the response I got back was Code 500, Internal error.

Why am I getting this response and again why is my previous code not working at all ?

Tanaike
  • 181,128
  • 11
  • 97
  • 165
DRW
  • 335
  • 1
  • 3
  • 17
  • don't use drive v2 – Linda Lawton - DaImTo Feb 14 '23 at 18:37
  • Even on v3 I also get bad request ? – DRW Feb 14 '23 at 18:45
  • any special reason you want to use batching? – Linda Lawton - DaImTo Feb 14 '23 at 20:00
  • try with 5 and let me know if there is an error – Linda Lawton - DaImTo Feb 14 '23 at 20:01
  • The xhr request worked when I chunked the files to 100, each but Im still looking to use the fetch option cause it is cleaner. But that option is still returning an error 400 bad request. Not understanding why it worked with xhr but not with fetch. this was with 12 files. And I need the batch request in anticipation of my users saving a large amount of files. – DRW Feb 14 '23 at 20:25
  • batching only saves you http request you understand not quote usage. I have personally only found batching to cause issues not solve any. – Linda Lawton - DaImTo Feb 14 '23 at 21:27
  • I understand, that it does not save on quota, But lets say for example I have a user that just keeps backing up his files untill they have 1000 files then decides to delete them from his drive to free up space or whatnot. deleting that many files one by one will take over an hour, where batch delete will take a few minutes. I don't see how that is better deleting one by one. I mean if there are issues with batching why even have it as an option. My issue I have tracked down to the fact that the Fetch response is not being created correctly If i can get that then it should work. – DRW Feb 14 '23 at 22:44
  • First, I apologize that my answer was not useful. About your question, I have 3 questions. 1. What language are you using? In your tag, it seems that `javascript` is used. You are using Javascript which is not Node.js. Is my understanding correct? 2. What is `files`? 3. You want to delete files. Your goal is only this. Is my understanding correct? – Tanaike Feb 14 '23 at 23:01
  • Yes using Javascript, files is an array containing files stored on google drive, my goal is to delete more than one file on google drive using the google REST API batch url – DRW Feb 14 '23 at 23:04
  • @DRW Thank you for replying. I apologize for my poor English skill. Unfortunately, from `Yes using Javascript, files is an array containing files stored on google drive`, I couldn't understand `files` in your script. By the way, when you add `@` to the user name like `@username`, the user can notice your reply. – Tanaike Feb 14 '23 at 23:24
  • @Tanaike files is an array of id's – DRW Feb 14 '23 at 23:26
  • @DRW Thank you for replying. From your reply, `files` is an array including the file IDs like `["fileId1", "fileId2",,,]`. In your goal, you want to delete files with the batch requests using the array `files` by the fetch API of Javascript. Is my understanding correct? By the way, your access token can be used for deleting files and you have permission for deleting those files? – Tanaike Feb 14 '23 at 23:30
  • @Tanaike welcome, yes you are correct in what I want to do , and yes I have the access token and permissions to delete the files. – DRW Feb 14 '23 at 23:32
  • @DRW Thank you for replying. I think that I could understand your question. I would like to prepare an answer. Please wait for it. – Tanaike Feb 14 '23 at 23:34
  • @DRW I posted an answer. Please confirm it. – Tanaike Feb 14 '23 at 23:50

1 Answers1

0

I believe your goal is as follows.

  • In your script, files is an array including the file IDs like ["fileId1", "fileId2",,,].
  • In your goal, you want to delete files with the batch requests using the array files by the fetch API of Javascript.
  • Your access token can be used for deleting files and you have permission for deleting those files.

In this case, how about the following sample script? In order to use the batch requests with Javascript, I have created a library. Ref In this answer, I would like to use this library.

Sample script:

<script src="https://cdn.jsdelivr.net/gh/tanaikech/BatchRequest_js@master/batchrequest_js.min.js"></script>
<script>
const accessToken = "###"; // Please set your access token.
const files = ["fileId1", "fileId2",,,]; // Please set your file IDs.

const object = {
  accessToken,
  batchPath: "batch/drive/v3",
  requests: files.map(id => ({ method: "DELETE", endpoint: "https://www.googleapis.com/drive/v3/files/" + id })),
};
Do(object)
  .then((e) => console.log(e))
  .catch((err) => console.log(err));
</script>

References:

Tanaike
  • 181,128
  • 11
  • 97
  • 165