How To Really Clean The Solution

I give you a small console-application, which removes all the Debug and Release folders under your Solution.


Normally you can clean your Solution properly, if you choose Clean Solution in the context-menu or from the Build-menu. But sometimes not all the files will be deleted. For example, if you change the target framework of a project, the old binaries will not be removed.
In my case, I have had a NETStandard library targeted netsandard1.6. The binaries were built in bin\Debug\netstandard1.6\

Then I had to change the target framework back to netstandard1.3. The new binaries were built in bin\Debug\netstandard1.3\
So if I choose "Clean Solution", the netstandard1.3 folder will be cleaned, but netstandard1.6 stays as it was.
The other projects in the solution (e.g WPF) have a project-reference on this netstandard project, and they still find the netstandard1.6 dlls and the changes I make after the switch do not have any effect.
But this is only one example when the solution will not correctly clean. If you have another case, please write it as comment.

The program
So I made a small console-application, which removes all the Debug* and Release* folders under the solution.
It is very simple, 
  1. class Program  
  2. {  
  3.     static void Main(string[] args)  
  4.     {  
  6.         var currentDir = Directory.GetCurrentDirectory();  
  7.         if (args.Length > 0)  
  8.             currentDir = args[0];  
  9.         var toDelete = Directory.GetDirectories(currentDir, "Debug*", SearchOption.AllDirectories)  
  10.             .Union(Directory.GetDirectories(currentDir, "Release*", SearchOption.AllDirectories)).ToArray();  
  12.         foreach (string dir in toDelete)  
  13.         {  
  14.             try  
  15.             {  
  16.                 Directory.Delete(dir, true);  
  17.                 Console.WriteLine(dir);  
  18.             }  
  19.             catch (Exception ex)  
  20.             {  
  21.                 Console.WriteLine($"CANNOT DELETE {dir}");  
  22.                 Console.WriteLine(ex.ToString());  
  23.             }  
  24.         }  
  26.         Console.WriteLine(new String('+', 80));  
  27.         Console.WriteLine($"Es wurden {toDelete.Length} Verzeichnisse gelöscht.");  
  28.         Console.ReadLine();  
  29.     }  
  30. }  
The solution-folder can be passed through a command line argument. If no argument, the current directory will be taken as root.
As you can see, I collect the folders with Debug* and Release* into a string-array. The SearchOption.AllDirectories do the collecting recursive in the subfolders, too.
Then with a foreach delete these directories.

Use it carefully

Use this code carefully! There is no warranty, if you delete important files in Debug or Release folders. I hope you do not have any important files under your Debug folder (databases or whatever)!

Use a Sourcecode control!

You can download this program from the attachment of this article.