Effective Strategies for Deleting Files & Directories During WiX Software Uninstallation
Effective Strategies for Deleting Files & Directories During WiX Software Uninstallation
Inno SetupWiX ToolsetInstallShieldMSIX Packaging Tool
Check your inbox and confirm the
subscription
Exclusive Newsletter
Exclusive Newsletter
Get the latest WiX insights direct to your inbox. Subscribe now!
Disclaimer: This post includes affiliate links
If you click on a link and make a purchase, I may receive a commission at no extra cost to you.
How To Remove Files and Folders when Uninstalling with WiX
Written by Alex Marin · May 27th, 2022
In some instances, when uninstalling an MSI, you may notice that some files and folders stay in the system and are not removed.
This can be seen in the example we portrayed in our articleHow To Embed CAB Files in MSI . There, we saw how to embed CAB files in an MSI with WiX Toolset and we created a simple MSI by using a basic WXS file.
If you tried that example and installed the MSI and then uninstalled it - you saw that the files still remained on the system.
In this article, we’re going to show you how to sort that out. Coming up, we will have a look at some options that you have with WiX to remove files and folders during uninstallation, and also how you can do the same with Advanced Installer.
How to Remove Files with WiX?
WiX offers the possibility to remove files with the RemoveFile element inside a component declaration.
For example, if we have a look at our original WXS, it should look something like this:
Copy
As mentioned, the RemoveFile element must be added under a component parent, like this:
Copy
This is configured so that all the files (“*.*”) inside our INSTALLDIR are deleted during uninstallation. You can specify only one file (“myfile.extension”) or a certain type of files by extension, using the wildcard character (“*.txt”).
The final WXS code will be:
Copy
Once we have the WXS created, let’s create the batch file to build the MSI:
“C:\Program Files (x86)\WiX Toolset v3.11\bin\candle.exe” test.wxs
“C:\Program Files (x86)\WiX Toolset v3.11\bin\light.exe” test.wixobj
@pause
Copy
Once you double-click the batch file, the MSI should be created.
If we install and uninstall the MSI, all the files should be removed during uninstallation and no files will remain in the system.
How to Remove Folders with WiX
To remove folders during the installation or uninstallation of an MSI, you have the option of using the RemoveFolder element. It works and behaves the same way as the RemoveFile element that we previously presented.
The steps are the same as when adding it to your WXS file.
But, the RemoveFolders element only works for directories created with the MSI database and with empty folders. This means that you must use the RemoveFile element to empty the folders first, and then the RemoveFolder element to delete the actual folder.
You may wonder - what about the folders that are created by the application after it is executed?
WiX has the option to add a RemoveFolderEX util extension element in your WXS. But adding this element is kind of tricky.
The RemoveFolderEX is a Custom Action that adds temporary rows to the RemoveFile table for each subfolder of the root specified in the WXS.
It has the characteristic that temporary rows must be written before the CostInitialize standard action, and MSI databases do not create properties for the directory hierarchy in your package until later, in the CostFinalize action. So, we need to get creative on how to add the RemoveFolderEx element in the WXS.
The easiest way to achieve this is to remember the directory we want to delete in the registry. Then, during the uninstallation - read the path from the registry and pass it to the RemoveFolderEx element. You can find a full step-by-step guide here: The WiX toolset’s Remember Property pattern .
How to remove directories using VBScript Custom Action?
An alternative would be to create a Custom Action with a simple VBScript code to delete the unwanted remaining directories. This way, you don’t have to worry about empty directories.
1. Let’s create a simple VBScript that will delete the folder, in our case “C:\Program Files (x86)\MyFirstInstaller” (you can pass properties to VBScript if you don’t want to hardcode your custom actions).
Dim FSO, Folder
set FSO=CreateObject(“Scripting.FileSystemObject”)
Folder=”C:\Program Files (x86)\MyFirstInstaller”
FSO.DeleteFolder(Folder)
Copy
In our case, the VBScript is called_Sample.vbs_ and it’s placed near the WXS and batch file we previously created.
2. Adjust the WXS file to include the custom action. The code to add our Custom Action is:
Copy
Before moving forward, let’s see what the above configuration does:
2.1 We declare our binary for the custom action as_Sample.vbs_
2.2 We create a Custom Action called_Sample.vbs_
2.3 We declare the execution to be deferred and ignore the return code
2.4 We place it before the _InstallFinalize_sequence (because we are running a deferred custom action)
2.5 We tell the MSI to run the custom action only during the remove operation by adding the following property :
REMOVE~=”ALL”
Copy
2.6 We suppress the Reboot
3. Now, it’s time to build the MSI by running the make_msi.bat file that we previously created and test the scenario.
4. We can see that the folder is now deleted after the uninstallation of the package.
As previously mentioned, you need to be aware of what you are trying to achieve in terms of files and folders removal in your MSI. For demo purposes, we used a hardcoded VBScript, which is not a best practice.
An ideal script would have the ability to get the MSI directory variables to be able to correctly remove the directories during uninstallation – if the user selects to install the application in a different directory.
Another important aspect is when and how you declare your custom action to run. In our example, we are deleting folders in “Program Files”, meaning that we need to declare the custom as action to run as deferred .
How to Remove files and folders with Advanced Installer?
Advanced Installer makes it easy to delete all the files you are adding to your project automatically during uninstallation. You don’t need to worry about declaring anything inside the project.
However, if the application is creating additional files that you want to remove during the uninstallation, use the File Removal Operations that is included in Advanced Installer’s GUI.
If you are curious about how removing files and folders works in Advanced Installer, try it through our 30-day full featured trial .
To achieve this, follow these steps:
1. Navigate to the folder that contains the file you want to remove, right-click on it and select New File Operation -File Removal.
2. A new dialog will appear where you can specify a file name, select a certain file type that you want to be deleted, and also choose if the operation happens during the installation or uninstallation of the MSI.
You also have two more options: to Remove Empty Folders or Remove Non Empty Folders.
3. Make sure to save your changes by clicking “OK”.
4. If you want to delete an entire folder or clean up additional folders left by your application, you can right-click on the folder and choose Properties.
5. A new dialog will appear which will have theOperations tab. Click on it.
You can choose to delete the folder whether it’s empty or not and select when the removing action should occur, as well as additional conditions and the overwrite behaviour.
As you can see, Advanced Installer’s GUI makes removal operations for files and folders a more seamless process.
Conclusion
And there you have it! Now you know how to remove files and folders when uninstalling with WiX, and have a few options to do so.
For direct access to the latest news, videos, and fresh HowTos and guides, join our mailing list to stay informed about the packaging industry: https://www.advancedinstaller.com/newsletter-subscribe.html
Let us know if there are any topics you’d like us to cover.
Advanced Installer #1 alternative to the WiX Toolset
We decided to focus on our main goal to save precious time of our developers.
HANS-LEO TEULINGS
PHD – CEO, NEUROSCRIPT, LLC
You might find useful:
- CAB Embedding Options in MSI
- Updated installers in WiX Toolset
- Custom Actions In WiX Toolset
- Windows Services with WiX Installer
- Set WiX Installer Version to the Current Build Version
- Remove Files and Folders when Uninstalling with WiX
- How to set a WiX Toolset custom action to run only on uninstall
- How to create non-advertised shortcuts for all users / per machine with WiX Toolset
- How to Remove Files on Uninstall Using WiX Toolset
- How to create major upgrades for MSI packages using WiX Toolset
- How to pass CustomActionData to a CustomAction using WiX
- How to build an installer without a license page using WiX Toolset
- How to create a WiX KeyPath attribute
- How to Create a Desktop Shortcut using WiX Toolset
- How to Use WiX MSI to Update an Installed Application
- How to correctly set the install folder in Wix
- WiX Toolset Visual Studio Extension
- How to create a custom dialog in WiX for user input
- How to create an MSI installer with WiX
- How to use WIX MSI to do a silent install or uninstall of a web app
Get the most from packaging with Advanced Installer
Try the 30-day fully-featured edition absolutely free!
No credit card required
Also read:
- [New] Accelerate or Slow Down on the Screen (Netflix)
- [New] InstaMixer Uniting Android & iOS Photo Sessions
- [New] Unrivaled Sonic Union for Speakers
- [Updated] Action Cam Showdown 2022 Hero5 Black Vs. Ultra 30 Streaming for 2024
- [Updated] Screen Recording Simplified Review Insights for 2024
- 2024 Approved Seamless Communication The Best 5 Webcams with Inbuilt Microphones
- Best 3 Nokia C12 Plus Emulator for Mac to Run Your Wanted Android Apps | Dr.fone
- Best in Class: The Top Wireless Travel Routers for Nomads
- Essential 8 Tips for Dust-Free, Spotless MacBook Care That Guarantees No Damage to Your Device
- Exciting New Enhancements Coming Soon: Microsoft Improves Snipping Tool & Paint on Windows 11
- Guide to Turning Off Windows 11'S Built-In Security: Microsoft Defender Explained
- In 2024, Mastering Android Device Manager The Ultimate Guide to Unlocking Your Oppo A58 4G Device
- Is the New Run0 Feature Phasing Out Sudo in the Linux World?
- Latest News Roundup: Changes to Netflix Subscription Options & Improved Solutions for iPhone Repairs
- Title: Effective Strategies for Deleting Files & Directories During WiX Software Uninstallation
- Author: Donald
- Created at : 2024-10-01 16:31:41
- Updated at : 2024-10-03 16:43:34
- Link: https://some-tips.techidaily.com/effective-strategies-for-deleting-files-and-directories-during-wix-software-uninstallation/
- License: This work is licensed under CC BY-NC-SA 4.0.