0

I'm running below script but it errors out with a Error Message of "Timed Out" because the folder Id I'm giving have lots of lots folders under.

Can someone please help me out to optimize it or alternative approach.

function listFolderContents() {
  var currentFolder = DriveApp.getFolderById("0B89Y-hAfWt_HVkhSbWprOVhPM00");
  traverseFolder(currentFolder);
}

function traverseFolder(folder) { 
  DriveApp.getRootFolder().removeFolder(folder);
  var subFolders = folder.getFolders();
  while (subFolders.hasNext()) {
    traverseFolder(subFolders.next());
  }
}
tehhowch
  • 9,645
  • 4
  • 24
  • 42
  • What is the desired behavior? – pneumatics May 04 '18 at 18:48
  • Considering that Drive folders are just labels, have you ensured that you are only visiting each folder once? Consider tracking the visited IDs via an object and only traversing folders that are not yet in the object. – tehhowch May 04 '18 at 20:11
  • Possible duplicate of [removeFolder doesn't work](https://stackoverflow.com/questions/40483398/removefolder-doesnt-work) – Rubén May 04 '18 at 20:54

2 Answers2

0

It seems like you are doing a recursive delete of a folder. Try moving the removeFolder call to after the recursive calls. Otherwise, you delete the folder before you call getFolders on it, which is why I think it's timing out on you.

Like this:

function deleteFolder(folder) { 
  var subFolders = folder.getFolders();
  while (subFolders.hasNext()) {
    deleteFolder(subFolders.next());
  }
  DriveApp.getRootFolder().removeFolder(folder);
}

The termination condition for the resursive function is that there are no sub-folders, that is you are in a leaf folder.

See this related answer to a question about recursive deletion of a binary tree

pneumatics
  • 2,836
  • 1
  • 27
  • 27
0

I think that solution in this case could be using of getContinuationToken() and example of iteration with continuationToken is available in another answer on stackoverflow.

edward
  • 243
  • 3
  • 13