Sometimes applications are creating files after an MSI installation has completed that are therefore not visible to the MSI setup. These files may need to be removed before install or at least on uninstall to free up disk space and for other cleanup reasons. In WiX Toolkit 3.6 and later there is a new feature named RemoveFolderEx to solve this problem very easily.
In case that you install or uninstall an application you are able to recursively remove directories without writing strange CustomActions for this task. For install I'm using it to upgrade from a manual "copy" type installation to an MSI version. It's also a must if you do not have any idea about the file names created in the application directories or simply for files left behind for unknown reasons. There are a few requirements if you'd like to use the tag in your WXS scripts.
- Extend the XML schema of your example.wxs file with the WiXUtilExtension:
<?xml version="1.0" encoding="utf-8"?> <Wix xmlns="http://schemas.microsoft.com/wix/2006/wi" xmlns:util="http://schemas.microsoft.com/wix/UtilExtension">
- For uninstall we need to read the application path from registry. This is required for an successful uninstall.
<!-- RemoveFolderEx requires that we "remember" the path for uninstall. Read the path value and set the APPLICATIONFOLDER property with the value. --> <Property Id="APPLICATIONFOLDER"> <RegistrySearch Key="SOFTWARE\$(var.Manufacturer)\$(var.SkuName)" Root="HKLM" Type="raw" Id="APPLICATIONFOLDER_REGSEARCH" Name="Path" /> </Property>
Save the application path on install in registry. This value need to be read on uninstall or the files are not added to the MSI database temporarily on uninstall and the files are therefore not removed. See RemoveFolderEx for more information about the reasons. Make sure you are adding this component with
to your scripts or it may not get executed on uninstall. A very important part on RemoveFolderEx is theutil
prefix in front of RemoveFolderEx. If you don't prefix you get warnings in candle and light and it does not work.If you need to cleanup on install (e.g. upgrading from NSIS setups):
<DirectoryRef Id="APPLICATIONFOLDER"> <Component Id="CleanupMainApplicationFolderOnInstall" Guid="{PUT-GUID-HERE}"> <Condition><![CDATA[Installed OR NOT MANUALPRODUCTFOUND]]></Condition> <RegistryValue Root="HKLM" Key="SOFTWARE\$(var.Manufacturer)\$(var.SkuName)" Name="Path" Type="string" Value="[APPLICATIONFOLDER]" KeyPath="yes" /> <util:RemoveFolderEx On="install" Property="APPLICATIONFOLDER" /> </Component> </DirectoryRef>
If you need to cleanup on uninstall only:
<DirectoryRef Id="APPLICATIONFOLDER"> <Component Id="CleanupMainApplicationFolder" Guid="*"> <RegistryValue Root="HKLM" Key="SOFTWARE\$(var.Manufacturer)\$(var.SkuName)" Name="Path" Type="string" Value="[APPLICATIONFOLDER]" KeyPath="yes" /> <!-- We need to use APPLICATIONFOLDER variable here or RemoveFolderEx will not remove on "install". --> <util:RemoveFolderEx On="uninstall" Property="APPLICATIONFOLDER" /> </Component> </DirectoryRef>
- Add these components to your MainApplication installation. Do not make them selectable as feature.
<Feature Id="MainApplication" ConfigurableDirectory="APPLICATIONFOLDER" Level="1" Title="$(var.SkuName)" Absent="disallow" Display="expand" AllowAdvertise="no" InstallDefault="local"> <ComponentRef Id="CleanupMainApplicationFolderOnInstall" /> <ComponentRef Id="CleanupMainApplicationFolder" /> <ComponentGroupRef Id="AppFiles" /> ... </Feature>
- Run candle and light with WixUtilExtension:
"%WIX%bin\candle.exe" example.wxs -ext WixUtilExtension "%WIX%bin\light.exe" example.wixobj -ext WixUtilExtension -out "ReleaseDir\example.msi"
I hope this helps you cleaning up your application folders on upgrades and uninstalls so nothing useless is left behind on your customer computers in future.