Build Vs. Rebuild Vs. Clean

“Build Solutions” and “Rebuild Solutions” are the most commonly used features in Visual Studio. As a developer, you write code, build solutions, and run applications in Visual Studio. You also often use a “rebuild solution” or a “clean then build solution”. Have you ever wondered why there are different options? When should they be used? Although many of you know the details, when asked, I see that many people have some confusion about it.

Let’s try to simplify it by knowing the answers to below questions.

  • What is the difference between Build and Rebuild?
  • When to use which option?
  • What does clean means, when one can use it?
Build Vs. Rebuild Vs. Clean

 One-liner Understanding:

  • Build Solution – Incremental build and compiles only the files that are changed.
  • Clean Solution – Deletes all compiled object, intermediate files.
  • Rebuild Solution – Deletes all compiled files and compiles all irrespective of changes.
Build Vs. Rebuild Vs. Clean

Build Solution

This is the fastest option to compile and run your solution in Visual Studio. When you select Build, Visual Studio performs an incremental build. What does it mean?

  • Build all assemblies that have modified files since the last build. If there is an assembly that has not undergone any changes, it will not be rebuilt.
  • During the Build option, Visual Studio will not delete any compiled files.

So for a single solution, if you create it for the first time and examine the detailed log in the output window, you should be able to see information similar to the one shown below.

Build Vs. Rebuild Vs. Clean

If there are no changes, and you will try to build it again, it will display message like below – which says. all the code bases are up to date.

Build Vs. Rebuild Vs. Clean

In another case if there are multiple projects and all are up-to-date projects then it will be skipped and only the changed files will be built.

Build Vs. Rebuild Vs. Clean

Clean Solution

This option will remove all complied files and clean up the output directories (bin / obj folders) from any previous builds.

Detailed view of Output Windows will display very similar information like shown in below screenshot.

Build Vs. Rebuild Vs. Clean

When we will execute, Clean Solutions, it will Cleans the project one by one.

Build Vs. Rebuild Vs. Clean
Build Vs. Rebuild Vs. Clean

Now, if you execute build immediate after the clean, you will see following steps of its execution (Post Clean Solution)

Build Vs. Rebuild Vs. Clean

Rebuild Solution

As we have discussed earlier, rebuild a solution is nothing but a clean and then build each project in the solution.

Build Vs. Rebuild Vs. Clean

Rebuilding a solution first will perform “Clean Solution” then execute the “Build Solution” for each project.

Note: The sequence of project execution from the list of projects will depends on the project dependency. Hence it may vary project to project.

Let summarize once again.

Build Solution – Incremental Build and Compiles only the files that are changed after the last build.

Clean Solution – It will delete all compiled, intermediate files.

Rebuild Solution – It will delete all compiled files and compiles them all irrespective of any change.

You can check more article on our website HERE under C# section or you can also check same on Micrsoft Docs too.

Leave a Reply

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