Share via


Delete a directory and all its contents

Question

Monday, August 21, 2006 1:01 PM | 2 votes

Hello.
I would like to delete a directory and all its contents.
The problem with Directory.Delete is that the folder is not empty and the deletion cannot be completed.

Any idea or suggestion?

All replies (4)

Monday, August 21, 2006 1:59 PM âś…Answered | 5 votes

Directory.Delete(@"C:\bleah", true);

System.IO.Directory.Delete has a 2nd overload that acepts a 2nd parameter that specifies whether to recursively delete the contents. The documentation for this can be seen here.


Tuesday, August 22, 2006 6:07 AM

didn't noticed that.

thank you a lot!


Thursday, April 8, 2010 7:58 PM

Here is a method to recursively delete all of the files and folders as you needed:

How To: Recursively Delete All Files and Directories in .NET:

http://allen-conway-dotnet.blogspot.com/2010/04/how-to-recursively-delete-all-files-and.html

 


Friday, June 22, 2018 12:18 PM

Actually I would like to thank you for this participation which indicates the correct understanding of the code