Difference between Build Solution, Rebuild Solution, and Clean Solution in Visual Studio?
Asked Answered
B

15

1328

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?

Boutique answered 22/6, 2010 at 18:17 Comment(3)
Also see #1247957Knightly
Is rebuild the same as clean then build?Elbow
@ColonelPanic yesUnionism
S
1122
  • 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.)

Sane answered 22/6, 2010 at 18:21 Comment(14)
@Jon - a Clean does wipe the bin and obj folders in my experience?Marcus
@womp: Not in the project I've just been looking at. It's still got all the assemblies there...Sane
@Jon - weird. I don't recall a clean ever not cleaning up those directories. I'm doing it right now and it's wiping all .dll and .pdb files. Definitely leaves my ReSharper junk alone though.Marcus
I have personally found "Clean Solution" to be more than unhelpful. If I really want it clean, manually deleting the bin and obj folders is the way to go. Have even been caught chasing phantom "errors" - until I did that. Clean just isn't reliable.Intercostal
if artifacts made their way through other means than through build artifacts (say for example a copy from another source integrated as a msbuidltask in the cproj) then clean leaves them be. Which makes it pretty much useless, I'd even say it is dangerous as it will leave you with a false sense of cleansiness.Quiet
Clean also removes add-in assemblies, registry entries and security settings - According to: msdn.microsoft.com/en-us/library/vstudio/… - cleaning up the project section.Jeffery
@Chris: Using tortoise-svn functionality "delete all unversioned files" as a replacement for VS clean works like a charm for me.Haft
@Tod: I have noticed in the past that Clean+Build works when Rebuild gives me link errors.Solita
@Solita I am not sure yet why that is. See Shivprasad Koirala for a possible answer. Though I'm not sure if it is the only reason why. I've read that rebuild does not clean the intermediate files, so I'm still confused as to what it does exactly.Koreykorff
I usually use "Clean" just to clean my project (for reduce size) before copy, archive or upload, or something like Github, etcSchweitzer
@verdana: For github etc, it's simpler to have a decent .gitignore file. But as per the answer, Clean doesn't always do a particularly thorough job in my experience.Sane
One reason to do a clean is so that COM registrations of the TLBs of DLLs can get unregistered. VS appears to take care of this. If you just delete the bin folder it would leave orphaned information in the registry.Barnette
it's easyer to have a step that destroys the build direcotry .. Clean build now has an Option for Remove all build dirdctories.. other wise clean JUST removes the source.Roper
I've found that a Rebuild will often delete PDB files of static immediately after they're created, causing linker warnings. Clean + Build doesn't suffer from that problem. This might be because I have a solution where the static library projects are used by more than one executable project. Perhaps that explains @happygilmore's linker errors.Participation
C
528

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

Carreno answered 22/7, 2013 at 6:24 Comment(0)
T
157

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.

Their answered 22/6, 2010 at 18:19 Comment(4)
Link? I thought the idea of a DLL, aka Dynamic Link Library, was to link at runtime?Shocker
"In practice you never need to Clean" <-- I call BS on this one.Hoecake
piers7 can you then provide a reason you would need to clean vs rebuild?Either
@Either You needn't look too far.Vitascope
P
60

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.

Phrasal answered 22/6, 2010 at 18:20 Comment(2)
What is intermediate files ? Do u mean Dll,pdb's and xml files ?Marcelmarcela
Used when you notice that Visual Studio didn't incorporate your changes in the latest assembly. Sometimes Visual Studio does make mistakes. — this is the reason I upvote this answer because no other answer seem to say about itNatch
A
19

I just think of Rebuild as performing the Clean first followed by the Build.

Aili answered 22/6, 2010 at 18:25 Comment(4)
This had no upvotes (until now) and according to the docs (see the link in Jon's answer) this is exactly right.Scylla
I don't think it does. I have a situation where doing a Clean Solution, followed by Build Solution works, but doing a Rebuild Solution fails. This is on a freshly created solution with 2 projects (one a dependent of the other).Pillage
@Pillage See Shivprasad's answer for the detail that makes the difference here. Rebuild cleans and then builds each individual project at a time, whereas running Clean first cleans everything out at once, then Build builds it all at once. I've run across instances where this change in clean/build order makes the difference between compiling and not compiling, too.Ballonet
@Ballonet Maybe it was caused by adding a project in your solution as a file reference instead of a project reference so the project build order didn't recognize it had to build a certain project before another, and the assembly didn't exist where it should have during the build?Perseid
M
8

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.

Malapert answered 22/6, 2010 at 18:20 Comment(0)
C
7

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.

Condemnation answered 23/7, 2018 at 18:5 Comment(1)
"Clean removes files/folders it has created in the past, but leaves anything that it didn't have anything to do with, initially." That's mostly, but not completely true. For intermediate files, yes, it deletes exactly the set of files that the toolchain produced. For others, in the build output directory, it uses wildcard patterns, like *.pdb. This leads to race conditions in Rebuild Solution that don't exist if you do Clean Solution followed by Build Solution.Participation
B
6

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.

Brochu answered 13/6, 2017 at 12:53 Comment(0)
H
6

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

Heavyhearted answered 14/12, 2019 at 7:57 Comment(0)
I
5

A two-part question ... but all answers (except from Justin Niessner) only focus on part 1: the difference. The second part is what I find more interesting: "When is the appropriate time to use each one of these?". But let me start with how I think of what each does:

Build: Should and usually does: generate (build) each intermediate and output file that is out-of-date with respect to its source files.

Rebuild: A workaround (hack) that allows you to build when the build command fails due to a bug with out-of-date evaluation.

Clean: A hacky implementation of deleting generated (intermediate and output) files so that a subsequent build might work. Hacky since it often doesn't delete enough. VS designers put all intermediate and output files in separate directories. Why not delete the directories?!?!?! But, I digress.

There is no clean/clear way to know when to use one command vs another. Knowing what each does, does not really inform me about the appropriate situation to use each.

It's more about personality than science. An optimist uses Build most of the time, but resorts to Rebuild if Build fails and they think the issue is wonky behavior of Visual Studio. And if Rebuild fails, then they do Clean and Build. When that fails, they start googling...

More pessimistic folks always use Rebuild since its more reliable even though takes longer when used every time. The most pessimistic always do Clean then Rebuild. They forgot there is a Build command. They also wear a belt and suspenders.

Some might think this is cynical. But, I think not. I think the UX of Visual Studio is bad and that users and the industry have gotten used to it. Worse, many other tools adopted the same UX since VS is ubiquitous.

IMO, Microsoft should: fix Build, fix Clean and eliminate Rebuild

Isfahan answered 19/12, 2022 at 21:27 Comment(0)
I
2

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.

Isfahan answered 10/9, 2018 at 3:46 Comment(1)
Specially in Xamarin projects I need to delete manually bin and obj folders to solve strange compilation errorsGraniteware
H
2

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.

Hussar answered 25/2, 2020 at 21:22 Comment(0)
M
2
**Build ,Rebuild, Clean Solution**

Clean Solution : deletes all compiled files (all dll’s and exe’s ).

Build Solution : compiles code files (dll and exe) that have changed.

Rebuild Solution : Deletes all compiled files and Compiles them again regardless of whether or not the code has changed.

Medicable answered 26/12, 2021 at 17:26 Comment(0)
S
1

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.

Storage answered 25/5, 2021 at 21:22 Comment(1)
Rebuild does Clean then Build project by project. If the intermediate or build output directories are shared by multiple projects in the same solution, that's different than Clean on the solution followed by Build on the solution.Participation
T
0

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.

Ticker answered 19/6, 2014 at 17:31 Comment(1)

© 2022 - 2024 — McMap. All rights reserved.