Learn more about SQL Server tools

mssqltips logo
 

Tutorials          DBA          Dev          BI          Career          Categories          Webcasts          Whitepapers          Today's Tip          Join

Tutorials      DBA      Dev      BI      Categories      Webcasts

DBA    Dev    BI    Categories

 

Cleanly Uninstalling Stubborn SQL Server Components


By:   |   Read Comments (24)   |   Related Tips: More > Install and Uninstall

Attend these FREE MSSQLTips webcasts >> click to register


Problem

There are scenarios where SQL Server is difficult or impossible to uninstall, upgrade, or replace (and these can block you from installing a new version or using a specific named or default instance):

  1. An expired Evaluation Edition:
    Evaluation period has expired. For information on how to upgrade your evaluation software please go to http://www.microsoft.com/sql/howtobuy


  2. An expired Management Studio:
    Your Microsoft SQL Server Management Studio evaluation period has expired.

    You can get a key to extend your trial by registering this copy of Microsoft Management Studio online. You may also purchase a key to activate the product.


  3. Unsupported operating system (after an OS upgrade):
    The operating system on this computer does not meet the minimum requirements for SQL Server xxxx.


  4. Missing MSI files, e.g.:
    Slp: Target package: "C:\...\sql_engine_core_inst.msi"
    Slp: InstallPackage: MsiInstallProduct returned the result code 2.


  5. Too many instances to remove individually, or other various errors during uninstall.

In these situations, is there a way to cleanly uninstall SQL Server?

Solution

There is a built-in command called msiexec which has an uninstall parameter (-x). This command can be used to remove stubborn programs through brute force. First, though, you need to get an inventory of the GUIDs that represent the programs you need to remove. All you see in the Control Panel are the friendly names, as you can see here:

Control Panel : Before

(This is a brand new system where I installed multiple SQL Server 2014 components in order to demonstrate my approach of cleanly removing them without using the Control Panel or SQL Server's own setup, but rather using msiexec and the GUIDs for the products.)

You can get the GUID associated with each installed product by searching around the registry, but I would rather use the MSI Inventory tool, which you can download (msiinv_new.zip) from this OneDrive folder, and extract msiinv.exe to a folder, say C:\temp\.

Once that file is there, you can open a PowerShell console, and run the following code:

c:\temp\msiinv.exe -s | Select-String "SQL Server" -Context 0,1

Here is a truncated version of what my output from that command looks like (there is a benign error you can ignore):

Powershell command output

This output is marginally useful for now; I can quickly scan the list and see all the GUID codes for the products I want to remove, and start mentally filtering any out as necessary. I could manually copy and change the output to feed each GUID individually to msiexec, but ultimately where I want to end up is a set of commands I can run directly from the command line, for example:

rem Microsoft SQL Server System CLR Types
msiexec /x "{C3F6F200-6D7B-4879-B9EE-700C0CE1FCDA}"

rem SQL Server 2014 Integration Services
msiexec /x "{327B1B40-2434-4DC5-9D4D-B9B24D4B2EDE}"

rem SQL Server 2014 SQL Data Quality Common
msiexec /x "{2D95D8C0-0DC4-44A6-A729-1E2388D2C03E}"
  
...

And I get there with a little help from my good buddy, co-worker, and PowerShell officionado, Allen White (@SQLRunr):

$a = c:\temp\msiinv.exe -s | Select–String "SQL Server" -Context 1,1;
$a = $a -replace "Product code:	","msiexec /x """;
$a = $a -replace ">", "rem";
$a = $a -replace "\t", "";
$a = $a -replace "}","}""";
$a | Out-File c:\temp\remove.bat -encoding ascii;

Now, you can open up remove.bat, look through the list, and remove any products you *don't* want to uninstall. The format will look like this, almost exactly how I wanted them:

rem Microsoft SQL Server System CLR Types
  msiexec /x "{C3F6F200-6D7B-4879-B9EE-700C0CE1FCDA}"
  
rem SQL Server 2014 Integration Services
  msiexec /x "{327B1B40-2434-4DC5-9D4D-B9B24D4B2EDE}"

rem SQL Server 2014 SQL Data Quality Common
  msiexec /x "{2D95D8C0-0DC4-44A6-A729-1E2388D2C03E}"

...

Now run remove.bat from the command line. You'll get prompts like this:

Windows Installer prompts

And you'll also likely see some confirmation dialogs from User Account Control. You may get some errors or dependency warnings, and to avoid some of the prompts, you'll want to make sure all related services have been manually stopped. You might want to run from an elevated command prompt (perhaps even using the "super admin" if you're on Windows 10), and also play with some of the switches to msiexec, like /quiet. You can enable /quiet by changing the second last line in the Powershell script above to:

$a = $a -replace "}","}"" /quiet";

Also, you may have to run the script twice to completely remove things that failed the first time due to dependency order. The second time around, if you don't use /quiet, you will see multiple notifications that the product you're trying to remove is no longer installed:

You can only remove a product once

But after running through it twice, this should leave you with a much cleaner Control Panel:

Control Panel : After

The remainder of these items should really be much easier to remove manually; or, you may not want to bother; or, you could repeat the process above with different arguments to Select-String. I wanted to focus on a single run-through with the SQL Server components, since they are the ones that prove most problematic.

Summary

This is not the most intuitive approach in the world, but I hope it helps some of you remove stubborn, sticky SQL Server components from your systems. It constitutes a bit more work than normal uninstall operations, but when Control Panel or SQL Server Setup won't cooperate, this might be the next best thing. (Note that this procedure will not be effective in completely removing a clustered instance of SQL Server; I wouldn't even try.)

Next Steps


Last Update:


signup button

next tip button



About the author
MSSQLTips author Aaron Bertrand Aaron Bertrand is a Senior Consultant at SQL Sentry, Inc., and has been contributing to the community for about two decades, first earning the Microsoft MVP award in 1997.

View all my tips
Related Resources





Post a comment or let the author know this tip helped.

All comments are reviewed, so stay on subject or we may delete your comment. Note: your email address is not published. Required fields are marked with an asterisk (*).

*Name    *Email    Notify for updates 


SQL tips:

*Enter Code refresh code     



Sunday, August 13, 2017 - 9:22:33 PM - Fabio Fernandes Back To Top

Thanks a lot for this EXCELLENT piece of technical knowledge, Mr. Bertrand.

I just wanted to let you know that this is absolutely up-to-date: I was struggling with an SQL Server 2017 installation that failed during the first setup, a few hours ago, and was getting crazy trying to fix it. As you wrote, it "stucked" in a strange state where Control Panel and SQL Server Setup can´t cooperate...

Thanks to a tip from "andrey-wd" user at stackoverflow, pointing to this great solution, now it´s working fine (after a re-installation, of course).

You rocks, man!

 


Wednesday, May 31, 2017 - 12:44:48 PM - Alexandru Agrapine Back To Top

 You've saved my life. I was getting quite fustrating with this :D

 

Thank you.

 


Saturday, April 29, 2017 - 5:49:48 AM - Fawad Raza Back To Top

 

Thank you :) Saved me a lot of time.


Friday, April 28, 2017 - 9:01:03 AM - Steve Thorn Back To Top

Lifesaver, indeed. Was able to cleanly remove an otherwise botched SQL Server 2008/2012/2014/2016 removal.


Friday, April 28, 2017 - 3:47:50 AM - Martin Goodrich Back To Top

 Thank you so much for sharing this with the community. For me it has proved to be a lifesaver.

 


Wednesday, April 26, 2017 - 8:32:10 AM - Adrian Strangfeld Back To Top

Actually, how did you found out about  msiexec?

 


Wednesday, April 26, 2017 - 8:22:31 AM - Adrian Strangfeld Back To Top

This saved my day, thank you.

 


Tuesday, April 18, 2017 - 5:31:46 PM - Stephen Back To Top

 Just wanted to say thanks so much for this process.  I'm learning powershell and working on automating our customer server setup and this is really great stuff.  Thanks for taking the time to share this with all of us.

 


Thursday, March 30, 2017 - 9:30:21 AM - Raznor Back To Top

This is pure genius.


Wednesday, March 22, 2017 - 8:01:23 AM - Niki Back To Top

Worked perfect. Thanks.

But how do I get rid of the installed SQL updates ?

You find them under "Programs and Features" -> "Installed Updates"

 


Tuesday, March 07, 2017 - 2:31:21 PM - Zvonimir Back To Top

 

 Thank you, worked flawless with MSSQL 2016.


Tuesday, February 21, 2017 - 2:48:31 AM - Bala Krishna Back To Top

Hi  Aaron

Thank you for this, this worked for me

 

Regards

 

 


Monday, November 07, 2016 - 11:03:59 AM - Marc Back To Top

 Hi Aaron,

I have been trying to use your blog post info to remove a number of failed SQL Server installs. I just tried your MSI inv tool and received an unusual response in power shell, I am guessing there are some values missing in the reg key...

 

      Microsoft SQL Server System CLR Types (x64)
           Product code:    Server System CLR Types (x64)
       Unexpected error: 1605 ()

Do you have any guidance for handling this result?

 

thanks

 


Friday, October 14, 2016 - 12:37:06 PM - Julian Back To Top

Thank you Aaron. Very helpful.

Cheers,

Julian 

 

 


Sunday, September 11, 2016 - 10:42:05 PM - John D (JJDemps) Back To Top

Aaron,

 

This blog post was awesome!!!  You saved me so much time.  I was having issues with SQL Server 2016 not uninstalling gracefully because it ran into an issue uninstalling R.  So, then the whole uninstall process got messed up with older versions of SQL and Shared components.  These steps allowed me to remove all of the left over installer msi files from the various version components.

 

One thing I would like to note though.  It seems the blog post area for some of the Powershell scripts may have caused an encoding issue with the "-" character.  The line of code is "$a = c:\temp\msiinv.exe -s | Select-String "SQL Server" -Context 0,1" in the Allen White code block section. This appears to lead to the "Select–String : The term 'Select–String' is not recognized as the name..." error some people have been posting in the comments section.  I was pulling my hair out for about a half hour trying to figure it out.  My initial thought it was a non-printable character issue somewhere, because when I retyped the "Select-String" command the script worked.  But, when I repasted the code and just changed the spaces around the "Select-String" it still didn't work.  When I changed only the "-" in "Select-String" it worked again.  I pasted the line in Notepad++ and used the ASCII -> HEX Converter plugin, the "-" = E28093 in HEX. I pasted line in again retyping "-" over the pasted one in "Select-String", the "-" = 2D in HEX which is the correct HEX value for that character. 

 

So for anyone getting the "Select-String not recognized" error, try retyping the "-" in the code line "$a = c:\temp\msiinv.exe -s | Select-String "SQL Server" -Context 0,1" if you copy and paste the code into the Powershell window or Powershell ISE.

 

Thanks again Aaron. I have bookmarked this for future reference.

 

-John


Friday, September 09, 2016 - 4:42:25 PM - Jason Back To Top

 Receiving this error in Step 2:

PS C:\Windows\system32> The term 'Select-String' is not recognized as the name
The term 'The' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:4
+ The <<<<  term 'Select-String' is not recognized as the name
    + CategoryInfo          : ObjectNotFound: (The:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException


Friday, August 12, 2016 - 11:30:37 AM - Josh Back To Top

 Just a quick question. When running this part.

 

$a = $a -replace "Product code:    ","msiexec /x """;
$a = $a -replace ">", "rem";
$a = $a -replace "\t", "";
$a = $a -replace "}","}""";

 

The .bat doesn't seem to be replacing any of the text. I need to go into the bat file and manually "Replace all" to get it to format correctly. Is there something i could be doing wrong?

 


Thursday, July 21, 2016 - 7:15:38 PM - Ayotunde Sodipe Back To Top

 

 

Aaron,

 

Thank you very much for your guide on "Cleanly Uninstalling Stubborn SQL Server Components" guide.

 

Recently, I installed SQL Server 2016 Express and it worked perfectly well on the day I installed it and I was able to do every database assignment I wanted to do.  The following day, I started the bank charges software (Banker) connected to the SQL Server Express but was unable to connect to the database.  I tried so many guides to resolve the problem but could not.  Then I decided to uninstall the SQL Server 2016 Express but the uninstall failed because some components were unremovable.  Then, I decided to re-install the SQL Server 2016 Express with the hope that the new installation would overwrite the files of the unremovable components but the re-installation failed because of the unremovable components.  As a result, I started searching google for guides on how to remove the stubborn/unremovable components and I came across many guides but all the ones I got before coming across your guide were cumbersome, difficult-to-implement, ineffective and failed.  When I applied your guide, the stubborn components were removed successfully.

 

Henceforth, I will recommend your guide to anyone having any stubborn SQL Server components.

 

Thank you very much again and God bless you.

 

Yours,

 

Ayotunde Sodipe.

 


Tuesday, November 17, 2015 - 4:43:32 PM - Aaron Bertrand Back To Top

Thanks Cristoph, but why can a statement terminator be used on every line but that one?


Tuesday, November 17, 2015 - 2:56:26 PM - Christoph Back To Top

I had the same problems with the select-string error and I found the error, there is one little mistake in the script.

Remove the first semicolon in the first line and then you can run it from ISE.

$a=c:\temp\msiinv.exe -s | Select-String "SQL Server" -Context 0,1
$a = $a -replace "Product code:    ","msiexec /x """;
$a = $a -replace ">", "rem";
$a = $a -replace "\t", "";
$a = $a -replace "}","}""";
$a | Out-File c:\temp\remove.bat -encoding ascii;

 


Thursday, October 08, 2015 - 8:42:35 AM - Peter Back To Top

I'm running Windows 8.1 and trying through the ISE, though pasting the same line in the command line below gives the same error. I can run just a Select-String without an issue, but piping to it is giving me fits for some reason.


Wednesday, October 07, 2015 - 6:29:57 PM - Aaron Bertrand Back To Top

Peter, what version of Windows, and where are you running the command (e.g. ISE vs. PS command line)?


Wednesday, October 07, 2015 - 5:13:30 PM - Peter Back To Top

Just can't seem to get this to work in Powershell on my box. When I start to run even the first line, I get "Select–String : The term 'Select–String' is not recognized as the name of a cmdlet, function, script file, or operable program."  Not really sure what's wrong with the code or my box. I tried switching the double-quotes to single-quotes in case that was part of the issue, but still no success.


Tuesday, October 06, 2015 - 4:09:17 PM - Tibor Nagy Back To Top

Hi Aaron,

Thank you for the great article. I just would like to add that in case strict corporate policies do not allow running msiinv.exe from an unverified source then you can still use this PowerShell command:

get-wmiobject Win32_Product | Format-Table IdentifyingNumber, Name

Regards:
Tibor


Learn more about SQL Server tools