1212
votes

What is the difference between Build Solution, Rebuild Solution, and Clean Solution in Visual Studio?

When is the appropriate time to use each one of these?

13
Is rebuild the same as clean then build?Colonel Panic
@ColonelPanic yesAlan Birtles

13 Answers

998
votes
  • Build solution will perform an incremental build: if it doesn't think it needs to rebuild a project, it won't. It may also use partially-built bits of the project if they haven't changed (I don't know how far it takes this)
  • Rebuild solution will clean and then build the solution from scratch, ignoring anything it's done before. The difference between this and "Clean, followed by Build" is that Rebuild will clean-then-build each project, one at a time, rather than cleaning all and then building all.
  • Clean solution will remove the build artifacts from the previous build. If there are any other files in the build target directories (bin and obj) they may not be removed, but actual build artifacts are. I've seen behaviour for this vary - sometimes deleting fairly thoroughly and sometimes not - but I'll give VS the benefit of the doubt for the moment :)

(The links are to the devenv.exe command line switches, but they do the same as the menu items.)

487
votes

Build solution: Compiles code files (DLL and EXE) which are changed.

Rebuild: Deletes all compiled files and compiles them again irrespective if the code has changed or not.

Clean solution: Deletes all compiled files (DLL and EXE file).

You can see this YouTube video (Visual Studio Build vs. Rebuild vs. Clean (C# interview questions with answers)) where I have demonstrated the differences and below are visual representations which will help you to analyze the same in more detail.

Build vs Rebuild

The difference between Rebuild vs. (Clean + Build), because there seems to be some confusion around this as well:

The difference is the way the build and clean sequence happens for every project. Let’s say your solution has two projects, “proj1” and “proj2”. If you do a rebuild it will take “proj1”, clean (delete) the compiled files for “proj1” and build it. After that it will take the second project “proj2”, clean compiled files for “proj2” and compile “proj2”.

But if you do a “clean” and build”, it will first delete all compiled files for “proj1” and “proj2” and then it will build “proj1” first followed by “proj2”.

Rebuild Vs Clean

152
votes

Taken from this link:

Build means compile and link only the source files that have changed since the last build, while Rebuild means compile and link all source files regardless of whether they changed or not. Build is the normal thing to do and is faster. Sometimes the versions of project target components can get out of sync and rebuild is necessary to make the build successful. In practice, you never need to Clean.

49
votes

Build Solution - Builds any assemblies which have changed files. If an assembly has no changes, it won't be re-built. Also will not delete any intermediate files.

Used most commonly.

Rebuild Solution - Rebuilds all assemblies regardless of changes but leaves intermediate files.

Used when you notice that Visual Studio didn't incorporate your changes in the latest assembly. Sometimes Visual Studio does make mistakes.

Clean Solution - Delete all intermediate files.

Used when all else fails and you need to clean everything up and start fresh.

19
votes

I just think of Rebuild as performing the Clean first followed by the Build. Perhaps I am wrong ... comments?

8
votes

Build solution will build any projects in the solution that have changed. Rebuild builds all projects no matter what, clean solution removes all temporary files ensuring that the next build is complete.

6
votes

Build Solution - Build solution will build your application with building the number of projects which are having any file change. And it does not clear any existing binary files and just replacing updated assemblies in bin or obj folder.

Rebuild Solution - Rebuild solution will build your entire application with building all the projects are available in your solution with cleaning them. Before building it clears all the binary files from bin and obj folder.

Clean Solution - Clean solution is just clears all the binary files from bin and obj folder.

6
votes

The one major thing I think people are leaving out is that Build and Clean are both tasks that are performed based on Visual Studio's knowledge of your Project/Solution. I see a lot of complaining that Clean doesn't work or leaves leftover files or is not trustworthy, when in fact, the reasons you say it isn't trustworthy actually makes it more trustworthy.

Clean will only remove (clean) files and/or directories that Visual Studio or the compiler themselves have in fact created. If you copy your own files or files/folder structures get created from an outside tool or source, then Visual Studio doesn't "know they exist" and therefore, should not touch them.

Can you imagine if the Clean operation basically performed a "del *.*" ? This could be catastrophic.

Build performs a compile on changed or necessary projects.

Rebuild performs a compile regardless of change or what's necessary.

Clean removes files/folders it has created in the past, but leaves anything that it didn't have anything to do with, initially.

I hope this elaborates a bit and helps.

5
votes

I have a a blank solution BuildRebuildClean and three class library Models,Repository,Notification.

I use Models and Repository in Notification class library.

Then:

  • Build solution Incremental build and compiles only the files that are changed. If an assembly has no changes, it won’t be re-built. Also, it will not delete any intermediate files. If Modify some code in Models library project, then BUILD solution. In the below screen shot, refer to the time stamp of DLL, EXE is updated in Models and Notification library.

enter image description here

  • Rebuild solution Deletes all compiled files and compiles all irrespective of changes, ignoring anything it’s done before. Right click on the solution name BuildRebuildClean. What it does is deletes all the assemblies, EXEs and referred files to compile again.

enter image description here

  • Clean Solution Deletes all compiled, intermediate files (i.e., EXEs and DLLs) from the bin/obj directory.

enter image description here

2
votes

All I know is a Clean does not do what "make clean" used to do - if I Clean a solution I would expect it delete obj and bin files/folders such that it builds like is was a fresh checkout of the source. In my experience though I often find times where a Clean and Build or Rebuild still produces strange errors on source that is known to compile and what is required is a manual deletion of the bin/obj folders, then it will build.

2
votes

This is concerning "Build Solution" option only.

I got totally fed up with Visual Studio's inability to really clean solutions and wrote this little tool that will do it for you.

Close your solution in VS first and drag its folder from Windows Explorer into this app or into its icon. Depending on the setting at the bottom of its window, it can also remove additional stuff, that will help if you try to manually upload your solution to GitHub or share it with someone else:

enter image description here

In a nutshell, it will place all "Debug" folders, Intellisense, and other caches that can be rebuilt by VS into Recycle Bin for you.

1
votes

Build solution only builds those projects which have changed in the solution, and does not effect assemblies that have not changed,

ReBuild first cleans, all the assemblies from the solution and then builds entire solution regardless of changes done.

Clean, simply cleans the solution.

0
votes

Clean will clean the artifacts in bin/Debug folder. Means deletes all the files in bin/Debug folder.

Build checks the artifacts in bin/Debug folder and if required then creates the artifacts (while checking for build time errors).

Rebuild = Clean + Build in a single go. This will first delete all the files in bin/Debug folder and then create the artifacts again in the bin/Debug folder.

One can confirm these operations by opening and observing the bin/Debug (or Release) folder and then Cleaning, building and rebuilding the project.