views:

11855

answers:

4

Hello,

I would like to modify an MSI installer (created through WIX) to delete an entire directory on uninstall.

I understand the there RemoveFile and RemoveFolder options in WIX, but these are not robust enough to recursively delete an entire folder that has content created after the installation.

I noticed the similar entry here (http://stackoverflow.com/questions/195919/removing-files-when-uninstalling-wix), but I was wondering if this could be done more simply using a call to a batch script to delete the folder.

This is my first time using WIX, and I'm still getting the hang of Custom Actions. Can anybody give me a basic example of a custom action that will run a batch script on uninstall?

Thanks

+13  A: 

You can do this with a custom action. You can add a refrence to your custom action under <InstallExecuteSequence>:

<InstallExecuteSequence>
...
  <Custom Action="FileCleaner" After='InstallFinalize'>Installed AND NOT UPGRADINGPRODUCTCODE</Custom>

Then you will also have to define your Action under <Product>:

<Product> 
...
  <CustomAction Id='FileCleaner' BinaryKey='FileCleanerEXE' ExeCommand='' Return='asyncNoWait'  />

Where FileCleanerEXE is a binary (in my case a little c++ program that does the custom action) which is also defined under <Product>:

<Product> 
...
  <Binary Id="FileCleanerEXE" SourceFile="path\to\fileCleaner.exe" />

The real trick to this is the Installed AND NOT UPGRADINGPRODUCTCODE condition on the Custom Action, with out that your action will get run on every upgrade (since an upgrade is really an uninstall then reinstall). Which if you are deleting files is probably not want you want during upgrading.

On a side note: I recommend going through the trouble of using something like C++ program to do the action, instead of a batch script because of the power and control it provides -- and you can prevent the "cmd prompt" window from flashing while your installer runs.

csexton
+4  A: 

The biggest problem with a batch script is handling rollback when the user clicks cancel (or something goes wrong during your install). The correct way to handle this scenario is to create a CustomAction that adds temporary rows to the RemoveFiles table. That way the Windows Installer handles the rollback cases for you. It is insanely simpler when you see the solution.

Anyway, to have an action only execute during uninstall add a Condition element with:

REMOVE ~= "ALL"

the ~= says compare case insensitive (even though I think ALL is always uppercaesd). See the MSI SDK documentation about Conditions Syntax for more information.

PS: There has never been a case where I sat down and thought, "Oh, batch file would be a good solution in an installation package." Actually, finding an installation package that has a batch file in it would only encourage me to return the product for a refund.

Rob Mensching
A: 

Thanks fuzzymonk! I was stuck with this remove=all and it never worked for upgrades!

+16  A: 

This topic has been a headache for long time. I finally figured it out. There are some solutions online, but none of them really works. And of course there is no documentation. So in the chart below there are several properties that are suggested to use and the values they have for various installation scenarios:

alt text

So in my case I wanted a CA that will run only on uninstalls - not upgrades, not repairs or modifies. According to the table above I had to use

<Custom Action='CA_ID' Before='other_CA_ID'>(NOT UPGRADINGPRODUCTCODE) AND (REMOVE="ALL")</Custom>

And it worked!

Thank you so much. This really helped me out.
Filip Lindboe
Are the values in that chart correct? Why would you need to add REMOVE="ALL"? NOT UPGRADINGPRODUCTCODE is only true for an uninstall (according to the chart), so (NOT UPGRADINGPRODUCTCODE) AND (REMOVE="ALL") would also only be true on an uninstall. The REMOVE="ALL" seems unnecessary.
Todd Ropog

related questions