Search code examples
wixlocaluninstallationappdata

WiX remove LocalAppData (AppData\Local\my_app_folder) on uninstall


I have a wix setup project which creates a ProgramMenu shortcut and a Desktop shortcut. I am able to remove these shortcuts by using RemoveFolder.

<!-- To remove Desktop shortcut -->
<RemoveFolder Id="RemoveDesktopFolder" Directory="DesktopFolder" On="uninstall"/>

<!-- To remove ProgramMenu shortcut-->
<RemoveFolder Id="CleanUpShortCut" Directory="ApplicationProgramsFolder" On="uninstall" />

However, on uninstall, I also want to be able to clear the current user's LocalAppData. More specifially, Users\CurrentUser\AppData\Local\my_application

So far, I have figured out that the RemoveFolder does not remove files recursively and that I would have to use util:RemoveFolderEx. This is how I have done it:

  <Directory Id="LocalAppDataFolder" Name="Local">
    <Directory Id="RemoveLocalData" Name="my_application">
      <Component Id="RemoveLocalAppData" Guid="PUT-GUID-HERE">
        <util:RemoveFolderEx On="uninstall" Property="RemoveLocalData"/>
        <RemoveFolder Id="RemoveLocalData" On="uninstall"/>
      </Component>
    </Directory>
  </Directory>

And I get this error:
ICE38: Component RemoveLocalAppData installs to user profile. It must use a registry key under HKCU as its KeyPath, not a file.

I figure I am not using RemoveFolderEx properly, but I do not know the right way in this case to clear my LocalAppData.

Note, I do not create the folder during installation. Instead, this [LocalAppData]\my_application is created post-installation at run-time by the application.


Solution

  • Finally managed to delete current user's LocalAppData(cache) with help from the following the link: https://pkisensee.wordpress.com/2015/10/06/windows-installer-removing-folders/

    In order to successfully delete the application's LocalAppData folder, I would have to search the registry (using RegistrySearch) for the location of the application cache folder and store it in a Property.

    <Property Id="CACHEFOLDER">
         <RegistrySearch Key="Software\CompanyName\AppName" Root="HKCU" Type="raw"
                         Id="CacheFolderRegSearch" Name="CachePath" />
    </Property>
    

    However, at the moment this registry entry does not exist, and the path would not be resolved. Hence during installation, I have to save the location of the LocalAppData cache folder in the registry, so that on uninstall, I can do a RegistrySearch and remove the cache folder recursively by util:RemoveFolderEx.

    To do that, I set the RegistryValue in a component as such:

    <Directory Id="TARGETDIR" Name="SourceDir">
    
        <!-- ... -->
    
        <!-- This is the name of the cache folder in LocalAppData -->
        <!-- In this case the cache folder is in \Users\CurrentUser\AppData\Local\MyAppCache -->
        <?define AppCacheFolder = "MyAppCache" ?>
        <Component Id="CacheCleanup" Guid="*">
             <RegistryValue Root="HKCU" Key="Software\CompanyName\AppName" Name="CachePath" 
                            Type="string" Value="[LocalAppData]$(var.AppCacheFolder)"                   
                            KeyPath="yes" />
             <util:RemoveFolderEx On="uninstall" Property="CACHEFOLDER"/>
        </Component>
    
        <!-- ... -->
    
    </Directory>
    

    Note that this is done within the TARGETDIR directory

    And finally, need to register that component into a Feature

    <Feature Id="MainApplication" Title="App Name" Level="1">
        <!-- Other Components -->
        <ComponentRef Id="CacheCleanup" />
    </Feature>