0

I'm using this in chrome extension.
Simple request with file ID will delete single file

xhr.open('DELETE', 'https://www.googleapis.com/drive/v2/files/' + ID, true);

If I want to delete multiple files from Google Drive, I'm looping trough ID's array and sending the same request, just multiple times.
Most of those requests succeed, but if I have more than 7-8, some of the request fail with error code 403 (I think that stand for forbidden).
Usually if I have 12 files to delete, two or three will fail.
(when I repeat requests, they are deleted)

Does Google Drive have some protection from throttling, and how do I delete multiple files?...
delaying with timers (eg. 100 ms) is not desirable, because I could have hundreds of files to delete, and it would take 10-30 seconds to process it

REST Drive API doc's doesn't say anything about deleting multiple files, only single

Wolf War
  • 1,503
  • 1
  • 15
  • 28
  • Well, you can simply retry the request if its status code is an error... – wOxxOm May 23 '17 at 18:40
  • @wOxxOm yea, I'm doing that :) ...but I don't think that's the right way... they (G-devs) must expected something like this to happen – Wolf War May 23 '17 at 18:43
  • The documentation recommends using [exponential back-off](https://developers.google.com/drive/v2/web/handle-errors) and/or [batch requests](https://developers.google.com/drive/v2/web/batch). Also, consider migrating to API v3 – Iván Nokonoko May 23 '17 at 18:48
  • @IvánNokonoko thx, I'll look into it – Wolf War May 23 '17 at 18:53
  • @IvánNokonoko example of batch request is missing from doc page :) bummer – Wolf War May 23 '17 at 19:34
  • You have one example in the [v3 documentation](https://developers.google.com/drive/v3/web/batch) – Iván Nokonoko May 23 '17 at 19:59
  • @IvánNokonoko about v3, are you sure I can use it with raw requests (from extension)? – Wolf War May 23 '17 at 20:52
  • Yes, I'm using it with `XMLHttpRequest`s without any problem – Iván Nokonoko May 23 '17 at 20:57
  • @IvánNokonoko I'm getting errors for every request if I put v3, but I guess it's not that simple. Is v2 about to be deprecated or something, when you advising switch to v3? – Wolf War May 23 '17 at 21:15
  • There are some changes between versions, you need to read the [documentation](https://developers.google.com/drive/v3/web/migration) carefully. I don't know if the v2 will be deprecated soon, but logically, if that happened, v2 would be deprecated before v3, I guess. You can try to port the v3 example to v2. – Iván Nokonoko May 23 '17 at 21:28

1 Answers1

4

This is a sample code of batch request to delete files from Google Drive using the REST API:

var arrayOfFileIds;  // array of id's of the files you want to delete
//notice that currently you can only batch up to 100 requests.
var authToken;  //your OAuth2 token.
var xhr = new XMLHttpRequest;
var boundary = "END_OF_PART";
var separation = "\n--"+boundary + "\n";
var ending = "\n--" + boundary + "--";

var requestBody = arrayOfFileIds.reduce((accum,current)=>{
           accum += separation +
           "Content-Type: application/http\n\n" +
           "DELETE https://www.googleapis.com/drive/v2/files/" +
           current +
           "\nAuthorization: Bearer " + authToken;
           return accum;
         },"") + ending;

xhr.onload = ()=>{
    console.log(xhr.response);
    //handle the response
};
xhr.open("POST", "https://www.googleapis.com/batch/drive/v2", true);
xhr.setRequestHeader("Content-Type","multipart/mixed; boundary=" + boundary);
xhr.send(requestBody);

Optionally, you can add a content-id header to every request (in requestBody above) to identify the individual responses in the batch response.

Iván Nokonoko
  • 4,888
  • 2
  • 19
  • 27