Delphi: Remove Directory Recursively

If you ended up looking for this, then you probably came to the conclusion that Windows.RemoveDirectory() can not remove a directory that has files/directories in it.

About.com proposes the following solution: http://delphi.about.com/cs/adptips1999/a/bltip1199_2.htm

I can assure you that this solution is valid and works. However, the code as it is proposed there will move the deleted folder into the recycle bin which is something you will want to avoid more often than not. The following code I modified to remove that effect:

Another way of doing this is recursing through all directories and removing all files before you remove the directories as seen here, but I imagine that might be slower as in the method above, the OS takes care of that internally:

One thought on “Delphi: Remove Directory Recursively

  1. Mitchell Hu say:

    if a parent directory has 2 empty sub-directory , this function only delete the 2 empty sub-directory, the parent will not be removed , right?
    It seems no way to deal this case in a single function , event recursively remove directory is impossible. Is there any good method to solve this problem?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.