Removing files when uninstalling WiX
Asked Answered
H

6

93

When uninstalling my application, I'd like to configure the Wix setup to remove all the files that were added after the original installation. It seems like the uninstaller removes only the directories and files that were originally installed from the MSI file and it leaves everything else that was added later in the application folder. In another words, I'd like to purge the directory when uninstalling. How do I do that?

Huffish answered 12/10, 2008 at 19:41 Comment(0)
A
90

Use RemoveFile element with On="uninstall". Here's an example:

<Directory Id="CommonAppDataFolder" Name="CommonAppDataFolder">
  <Directory Id="MyAppFolder" Name="My">
    <Component Id="MyAppFolder" Guid="*">
      <CreateFolder />
      <RemoveFile Id="PurgeAppFolder" Name="*.*" On="uninstall" />
    </Component>
  </Directory>
</Directory>

Update

It didn't work 100%. It removed the files, however none of the additional directories - the ones created after the installation - were removed. Any thoughts on that? – pribeiro

Unfortunately Windows Installer doesn't support deleting directories with subdirectories. In this case you have to resort to custom action. Or, if you know what subfolders are, create a bunch of RemoveFolder and RemoveFile elements.

Aprilette answered 12/10, 2008 at 21:38 Comment(10)
Thank you Pavel. However it didn't work 100%. It removed the files, however none of the additional directories - the ones created after the installation - were removed. Any thoughts on that?Huffish
Oh, neither the files under those directories were deleted.Huffish
When you will keep files (config files for example) in 'MyAppFolder' on Major Upgrades, you will get problems with this approach. All files will be removed by an Upgrade.Recidivate
This will cause problem with shared components: they will be removed regardless of other usages still active.Chyle
Is there possible with your code to create a Directory in MyAppFolder ? When I add a directory after </Component> I have an compilation failed Found orphaned Component 'MyAppFolder'.Axolotl
@Axolotl Please ask a separate question for your problem and include the full error text.Aprilette
@PhilipRego See msdn.microsoft.com/en-us/library/windows/desktop/aa367992.aspx for CommonAppDataFolder documenation.Aprilette
Thanks to Pavel, I added more complete answer (100% success) hereFenner
I had a problem removing top folder when unistalling simply because I was in that directory in a cmd prompt. How do you handle this case?Thunderous
this solution no longer works: Found orphaned Component 'MyAppFolder'. If this is a Product, every Component must have at least one parent Feature. To include a Component in a Module, you must include it directly as a Component element of the Module element or indirectly via ComponentRef, ComponentGroup, or ComponentGroupRef elements.Mazzard
G
34

Use RemoveFolderEx element from Util extension in WiX.
With this approach, all the subdirectories are also removed (as opposed to using RemoveFile element directly). This element adds temporary rows to RemoveFile and RemoveFolder table in the MSI database.

Goodin answered 7/5, 2012 at 6:20 Comment(4)
Warning: When using RemoveFolderEx on="uninstall", it also removes the folder on an upgrade (Wix 3.9). Same behaviour on RemoveFile and RemoveFolder. If you want to keep files on an upgrade, you cannot use all these approaches.Recidivate
@Recidivate what approach would you suggest if you wanted to keep the files on an upgrade?Hideandseek
@Bijington: When you want to keep specific files in your installation folder on an upgrade, then use an custom action which executes custom code (e.g. c# written HandleSetup.exe). The custom code delivers your files on install, keeping your files on upgrade and removes the files on uninstall.Recidivate
@Recidivate thanks, I may have to look into that approach although this one is currently working: https://mcmap.net/q/104643/-how-to-keep-a-config-file-when-major-upgrade-in-wix-v3-8Hideandseek
F
13

To do this, I simply created a custom action to be called on uninstall.

The WiX code will look like this:

<Binary Id="InstallUtil" src="InstallUtilLib.dll" />

<CustomAction Id="DIRCA_TARGETDIR" Return="check" Execute="firstSequence" Property="TARGETDIR" Value="[ProgramFilesFolder][Manufacturer]\[ProductName]" />
<CustomAction Id="Uninstall" BinaryKey="InstallUtil" DllEntry="ManagedInstall" Execute="deferred" />
<CustomAction Id="UninstallSetProp" Property="Uninstall" Value="/installtype=notransaction /action=uninstall /LogFile= /targetDir=&quot;[TARGETDIR]\Bin&quot; &quot;[#InstallerCustomActionsDLL]&quot; &quot;[#InstallerCustomActionsDLLCONFIG]&quot;" />

<Directory Id="BinFolder" Name="Bin" >
    <Component Id="InstallerCustomActions" Guid="*">
        <File Id="InstallerCustomActionsDLL" Name="SetupCA.dll" LongName="InstallerCustomActions.dll" src="InstallerCustomActions.dll" Vital="yes" KeyPath="yes" DiskId="1" Compressed="no" />
        <File Id="InstallerCustomActionsDLLCONFIG" Name="SetupCA.con" LongName="InstallerCustomActions.dll.Config" src="InstallerCustomActions.dll.Config" Vital="yes" DiskId="1" />
    </Component>
</Directory>

<Feature Id="Complete" Level="1" ConfigurableDirectory="TARGETDIR">
    <ComponentRef Id="InstallerCustomActions" />
</Feature>

<InstallExecuteSequence>
    <Custom Action="UninstallSetProp" After="MsiUnpublishAssemblies">$InstallerCustomActions=2</Custom>
    <Custom Action="Uninstall" After="UninstallSetProp">$InstallerCustomActions=2</Custom>
</InstallExecuteSequence>

The code for the OnBeforeUninstall method in InstallerCustomActions.DLL will look like this (in VB).

Protected Overrides Sub OnBeforeUninstall(ByVal savedState As System.Collections.IDictionary)
    MyBase.OnBeforeUninstall(savedState)

    Try
        Dim CommonAppData As String = Me.Context.Parameters("CommonAppData")
        If CommonAppData.StartsWith("\") And Not CommonAppData.StartsWith("\\") Then
            CommonAppData = "\" + CommonAppData
        End If
        Dim targetDir As String = Me.Context.Parameters("targetDir")
        If targetDir.StartsWith("\") And Not targetDir.StartsWith("\\") Then
            targetDir = "\" + targetDir
        End If

        DeleteFile("<filename.extension>", targetDir) 'delete from bin directory
        DeleteDirectory("*.*", "<DirectoryName>") 'delete any extra directories created by program
    Catch
    End Try
End Sub

Private Sub DeleteFile(ByVal searchPattern As String, ByVal deleteDir As String)
    Try
        For Each fileName As String In Directory.GetFiles(deleteDir, searchPattern)
            File.Delete(fileName)
        Next
    Catch
    End Try
End Sub

Private Sub DeleteDirectory(ByVal searchPattern As String, ByVal deleteDir As String)
    Try
        For Each dirName As String In Directory.GetDirectories(deleteDir, searchPattern)
            Directory.Delete(dirName)
        Next
    Catch
    End Try
End Sub
Frizzell answered 6/11, 2008 at 21:39 Comment(0)
U
13

Here's a variation on @tronda's suggestion. I'm deleting a file "install.log" that gets created by another Custom Action, during Uninstall:

<Product>
    <CustomAction Id="Cleanup_logfile" Directory="INSTALLFOLDER"
    ExeCommand="cmd /C &quot;del install.log&quot;"
    Execute="deferred" Return="ignore" HideTarget="no" Impersonate="no" />

    <InstallExecuteSequence>
      <Custom Action="Cleanup_logfile" Before="RemoveFiles" >
        REMOVE="ALL"
      </Custom>
    </InstallExecuteSequence>
</Product>

As far as I understand, I can't use "RemoveFile" because this file is created after the installation, and is not part of a Component Group.

User answered 7/7, 2013 at 15:37 Comment(6)
I did use this solution, with some changes to remove the whole directory: ExeCommand="cmd /C RD &quot;[INSTALLFOLDER]&quot; /s /q"Lunisolar
@Lunisolar how to delete INSTALLFOLDER, on win 10 it gets deleted but on Windows server 2012 it does not.Okelley
Great solution. Thanks!Computation
I tried a number of things - one wouldn't think that it could be that difficult to remove a single file during uninstall. This worked for me though - thanks!Anglophobia
Can you explain what the REMOVE="ALL" does? Shouldn't the uninstall process remove the files after this custom action?Denunciate
@Denunciate I don't know what that does. It's just voodoo that I copied from somewhere.User
F
8

This would be a more complete answer for @Pavel suggestion, for me it's working 100%:

<Fragment Id="FolderUninstall">
    <?define RegDir="SYSTEM\ControlSet001\services\[Manufacturer]:[ProductName]"?>
    <?define RegValueName="InstallDir"?>
    <Property Id="INSTALLFOLDER">
        <RegistrySearch Root="HKLM" Key="$(var.RegDir)" Type="raw" 
                  Id="APPLICATIONFOLDER_REGSEARCH" Name="$(var.RegValueName)" />
    </Property>

    <DirectoryRef Id='INSTALLFOLDER'>
        <Component Id="UninstallFolder" Guid="*">
            <CreateFolder Directory="INSTALLFOLDER"/>
            <util:RemoveFolderEx Property="INSTALLFOLDER" On="uninstall"/>
            <RemoveFolder Id="INSTALLFOLDER" On="uninstall"/>
            <RegistryValue Root="HKLM" Key="$(var.RegDir)" Name="$(var.RegValueName)" 
                    Type="string" Value="[INSTALLFOLDER]" KeyPath="yes"/>
        </Component>
    </DirectoryRef>
</Fragment>

And, under Product element:

<Feature Id="Uninstall">
    <ComponentRef Id="UninstallFolder" Primary="yes"/>
</Feature>

This approach set a registry value with the desired path of the folder to be deleted on uninstall. At the end, both INSTALLFOLDER and registry folder are removed from the system. Note that the path to the registry can be at other hive and other locations.

Fenner answered 16/11, 2015 at 13:16 Comment(0)
P
6

Not an WIX expert, but could a possible (simpler?) solution to this be to run the Quiet Execution Custom Action which is part of the built in extensions of WIX?

Could run the rmdir MS DOS command with the /S and /Q options.

<Binary Id="CommandPrompt" SourceFile="C:\Windows\System32\cmd.exe" />

And the custom action doing the job is simple:

<CustomAction Id="DeleteFolder" BinaryKey="CommandPrompt" 
              ExeCommand='/c rmdir /S /Q "[CommonAppDataFolder]MyAppFolder\PurgeAppFolder"' 
              Execute="immediate" Return="check" />

Then you'll have to modify the InstallExecuteSequence as documented many places.

Update: Had issues with this approach. Ended up making a custom task instead, but still considers this a viable solution, but without getting the details to work.

Publican answered 2/2, 2010 at 12:47 Comment(4)
I like this option bar the fact that you are including the cmd.exe in the installer. Surely every machine will have it, you just need to use a DirectorySearch to find it! :)Ertha
Don't do this. 1) you are embedding cmd.exe into your installer. 2) You are making changes to the system during script generation 3) There is no rollback option 4) Doesn't deal with locked files correctlyWarranty
I have doubts that it's legal to distribute a file from the Windows installation. It's also unclear if it will work on the target system which may run a different version of Windows.Rheology
No files has been distributed. It uses the files installed on the OS.Publican

© 2022 - 2024 — McMap. All rights reserved.