Click here to Skip to main content
15,895,142 members
Articles / Programming Languages / C#
Tip/Trick

How to Really Clean the Solution

Rate me:
Please Sign up or sign in to vote.
2.76/5 (7 votes)
11 Oct 2017CPOL2 min read 17.7K   79   4   10
How to really clean your Visual Studio Solution

Introduction

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

Background

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 a comment.

Using the Code

So I made a small console-application, which removes all the Debug* and Release* folders under the solution.

It is very simple:

C#
class Program
{
    static void Main(string[] args)
    {
        var currentDir = Directory.GetCurrentDirectory();
        if (args.Length > 0)
            currentDir = args[0];
        var toDelete = Directory.GetDirectories(currentDir, "Debug*", SearchOption.AllDirectories)
            .Union(Directory.GetDirectories
                  (currentDir, "Release*", SearchOption.AllDirectories)).ToArray();

        foreach (string dir in toDelete)
        {
            try
            {
                Directory.Delete(dir, true);
                Console.WriteLine(dir);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"CANNOT DELETE {dir}");
                Console.WriteLine(ex.ToString());
            }
        }

        Console.WriteLine(new String('+', 80));
        Console.WriteLine($"{toDelete.Length} folders are removed.");
        Console.ReadLine();
    }
}

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.

Use It Carefully

Use this code carefully! There is no warranty if you delete important files in Debug or Release folders.

Before you start this program, you should close the solution in VS! Otherwise you get an exception, that the folder is not empty.

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 tip.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 3 Pin
Jim_Snyder11-Oct-17 3:51
professionalJim_Snyder11-Oct-17 3:51 
QuestionNuke it from orbit, it's the only way to be sure... Pin
dandy7210-Oct-17 5:42
dandy7210-Oct-17 5:42 
AnswerRe: Nuke it from orbit, it's the only way to be sure... Pin
wkempf10-Oct-17 7:59
wkempf10-Oct-17 7:59 
QuestionBatch file Pin
tbayart10-Oct-17 3:52
professionaltbayart10-Oct-17 3:52 
I have a batch file to recursively remove 'bin' and 'obj' folders.
Adapted to your need, the following 2 lines put in a batch file (ie : clean.bat) in the solution folder will remove all 'Release' and 'Debug' folders
FOR /F "tokens=" %%G IN ('DIR /B /AD /S Release') DO RMDIR /S /Q "%%G"
FOR /F "tokens=" %%G IN ('DIR /B /AD /S Debug') DO RMDIR /S /Q "%%G" 

AnswerRe: Batch file Pin
Laszlo Voros11-Oct-17 20:44
Laszlo Voros11-Oct-17 20:44 
QuestionPowerShell One Liner Pin
wkempf9-Oct-17 4:01
wkempf9-Oct-17 4:01 
AnswerRe: PowerShell One Liner Pin
Laszlo Voros9-Oct-17 4:23
Laszlo Voros9-Oct-17 4:23 
GeneralRe: PowerShell One Liner Pin
wkempf9-Oct-17 7:24
wkempf9-Oct-17 7:24 
GeneralRe: PowerShell One Liner Pin
PeejayAdams10-Oct-17 2:47
PeejayAdams10-Oct-17 2:47 
GeneralRe: PowerShell One Liner Pin
wkempf10-Oct-17 7:12
wkempf10-Oct-17 7:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.