Is it safe to delete files using FolderItem.Children?

Consider this code:

For Each Child As FolderItem In Folder.Children
  Child.Delete
Next

Is this safe? I know using indexes to work backwards of course. But using an iterator, would files still be skipped? Would deleting files change the iterator contents and throw an exception somewhere?

Just curious. I’m going to do it the old fashioned way because I’m too lazy to test it.

Interesting question for which I don’t have an answer, but I wanted to point out you have to watch out for aliases and symbolic links that may delete unwanted items.

1 Like

Well, only testing will answer your question. Not sure if the iterator is more than syntactic sugar.

I guess it delete only the file but not the object that this iterator is untouched.

Funny, I was asking myself this question yesterday too. :slight_smile:
If someone can confirm this is working… that would be great (I’m lazy too :wink:

For Each Child As FolderItem In Folder.Children(False)
  Child.Delete
Next

I would add the (false) which means TrueFolderItem in API1 because without (False), then with (True), if you have alias in your folder, you won’t remove it but its target.

1 Like

Even running it as a test wont prove it worked 100% reliably - you cant be certain that the order is repeatable.

Create an array of folderitems using that loop.
Then loop through your array, deleting as you go.
No index problem can occur

Afterwards, check to see if any new files have been created while you were working.

1 Like

for me it works at 2020r2.1 and linux.
because i had the need for deleting older data from security cam i made a method even
and it runs fine with for each and .Remove