Remove all files and subfolders from directory

How do I remove all files and folders from a given directory.  I want to clean out an existing IIS directory without removing the root folder.  Do I need to iterate over each folder and do a seperate delete tree on it?  If so, how do I this?

Hi John,

Thanks for your post. Delete Tree is recursive, so if you use the Delete Directory action with "Delete Tree" then it should get rid of everything.

Regards,

Angus

Thanks for the reply, but I think you have missed the question.  I do not want to delete the top level folder.  I want to delete everything in the top level folder (both files and directories) and still have the top level folder when I am done.  How can I do this?

Hi John,

You’re right, I did miss the question. Sorry.

Is deleting the directory and then recreating it an option? That’s probably the simplest solution - Delete Directory then Create Directory.

Regards,

Angus

I think it is an option and i will try it that way.  I still think it would be nice to have a 'Clear Directory' type of action. Thanks.

I’ll add a recursive option to “Delete Files” to our to-do list, and note that deleting empty directories would be good as well. This would give you the results you’re after.

Regards,

Angus

 Hi, has anyone been able to figure this out? I'd still like to see a way to delete all files and subdirectories within a folder, but not the folder itself.

Deleting the folder and recreating it is not an option because I'd like to retain the security and share permissions on the folder.

Hi Sameer,

I've attached an example project that deletes all files and sub-directories within a folder.

Regards,
Paul.
 

DirectoryCleaner.fbz7 (2.099 KB)

Paul, great solution! Thanks so much! Didn’t even look in the iterators section…

I’m going to try out the directory cleaner method because I was coming here to report a problem I’d had with doing similar. It “works” to some degree when we tell it to delete tree followed immediately by a command to recreate the directory. However, it frequently fails saying that it can’t make the directory. I don’t know if it’s because it happens too fast or what.

I’m contemplating just having it pause after it executes the delete action, but I’d really rather not. Ideally I wouldn’t have it log every file deleted since it’s well over 1000 files, but perhaps that is something I could try to slow it down. (I’m using FB v 7.0.0.1753)