Delete file or folder speed

Hi, I believe I’m doing something wrong but function seems pretty obvious so I’m not sure what exactly is wrong. So the function is:

private async cleanup(folder: uxp.Folder): Promise<void> {
  const folders = await folder.getEntries();
  const tasks = folders.map(async (sub): Promise<void> => {
    if (sub.isFolder) {
      await this.cleanup(sub as uxp.Folder);
    }
    await sub.delete();
  });

  await Promise.all(tasks);
}

Pretty obvious. I have folder with like 50 folders and about 5-7 entries within each folder. This function took several minutes to remove them. Did I miss something? Is there any way to speed it up?

deleting files is an expensive operation, so I’d assume the function can take a while if you have a lot of files to delete. @kerrishotts any recommendations on speeding up the process?

Just to keep up to date I slightly adjusted function, I didn’t change speed much but anyway:

private async cleanup(folder: uxp.Folder): Promise<void> {
  const entries = await folder.getEntries();
  const self = this;
  const filesTask = entries.filter(e => !e.isFolder).map(e => e.delete());
  const foldersTask = entries.filter(e => e.isFolder).map(e => self.cleanup(e as uxp.Folder));
  await Promise.all(foldersTask);
  const tasks = entries.map((e) => e.delete());
  await Promise.all(tasks);
  await Promise.all(filesTask);
}

looks redundant, and maybe failing (causing slowdowns), because you’re already handling the files at this level in filesTask and the sub-folders in foldersTask.

Thanks for heads up! Updated the function below. But performance seems to be the same (

private async cleanup(folder: uxp.Folder): Promise<void> {
  const entries = await folder.getEntries();
  const files = entries.filter(e => !e.isFolder);
  const folders = entries.filter(e => e.isFolder);
  let foldersTask = folders.map(e => this.cleanup(e as uxp.Folder));
  const filesTask = files.map(e => e.delete());
  await Promise.all(foldersTask);
  foldersTask = folders.map((e) => e.delete());
  await Promise.all(foldersTask);
  await Promise.all(filesTask);
}

This takes several minutes? Ouch.

What OS are you running?

Also, could you instrument this a little to see where the delays are?

And, it would be helpful to see a directory structure (even if simulated) to get an idea for how deep you’re having to recurse and such.

Sorry for the delay, here’s the result of the run:
Cleanup finished in 150323 ms. I removed 510 files and 177 folders with total size of 110Mb.
OS is Windows 10 x64 version 1903.
Directory structure consists of 50 folder, each folder has 1 file and 1-5 folders. Each subfolder contains 4-5 files. So it’s pretty simple structure, production structure will contain a way more files.