Skip to content Skip to sidebar Skip to footer

How Do I Delete A List Of Files Meeting Criteria With Google Drive Api And Javascript?

I want to get a list of things with get list (which I have already coded in), but next, I want a delete button that deletes all the things on that list. How can I do that with Goog

Solution 1:

So using the Javascript Drive API, you will first issue a GET to the files endpoint, using the list action which supports your query parameter q. It sounds like you already have that figured out.

You will get a response object -- to get more specific, you would need to post some code. Are you using VanillaJS XMLHttpRequest, jQuery's $.ajax(), perhaps the delicious and native async fetch() API? Either way, you will get a response which can be parsed to JSON.

As you have discovered, the Drive API does not support permanently deleting multiple Drive resources in one request. At this point, you may be rethinking the architecture of your application: removing a very long list could require many HTTP requests. This could generate significant overhead, so you might want to limit how long lists can be or do something else clever. If you decide to take this route, I'd suggest using fetch to use the API asynchronously.

So after you call JSON.parse(responseBody) (or responseBody.json() if you're using fetch()), you will get something from Google API that looks like this:

{
 "kind": "drive#fileList",
 "etag": "...",
 "selfLink": "https://www.googleapis.com/drive/v2/files",
 "nextPageToken": "...",
 "nextLink": "...",
 "incompleteSearch": false,
 "items": [
  {
   "kind": "drive#file",
   "id": "SOME FILE ID",
   "etag": "...",
   "selfLink": "...",
   "alternateLink": "...",
   "embedLink": "...",
   "openWithLinks": {
        ...
   },
   {
   "kind": "drive#file",
   "id": "SOME FILE ID",
   "etag": "...",
   "selfLink": "...",
   "alternateLink": "...",
   "embedLink": "...",
   "openWithLinks": {
        ...
   }
]

So what you need is:

  1. A function which:

    • Is called when your GET to files.list is complete.
    • Loops through responseJSON.items and calls the function below with an itemId.
  2. A function which issues an HTTP DELETE given an ID.

So without any specific details of your implementation, a general solution for your purpose might look like:

var apiEndpoint = new URL("https://www.googleapis.com/drive/v2/files"),
    params = {q: "some stuff", otherParams: "lol"}

// assemble GET querystring in a nice wayObject.keys(params).forEach(function(key) {
   apiEndpoint.searchParams.append(key, params[key]);
});

// go and fetch! (explicit HTTP header options optional but good practice)
fetch(apiEndpoint , {
                 method: 'GET',
                 mode: 'cors',
                 redirect: 'follow',
                 headers: new Headers({
                     'Content-Type': 'application/json'
                 })
             });
                 .then(function (responseBody) {
                     return responseBody.json();
                 })
                 .then(function (responseObj) {
                     for(i=0; i<responseObj.items.length; i++) {
                         asyncDeleteDriveItem(responseObj.items[i].id);
                     }
                 });

        });

functionasyncDeleteDriveItem() {/* ... */}

Post a Comment for "How Do I Delete A List Of Files Meeting Criteria With Google Drive Api And Javascript?"