Delete registry key if empty on uninstall - Advanced Installer Community
Alin.Sarivan
Posts: 13
Joined: Thu Jul 31, 2014 7:16 am

Delete registry key if empty on uninstall

Hi guys,

Our software writes the following keys in the registry:
HKLM\Software\<Company_Name>\<Product_Name>\<Component_Name>

Components can be installed separately.
So far, on uninstall of a component, we delete <Component_Name> key, but I want to delete also <Company_Name>\<Product_Name>, if and only if the key is empty.

Our product uses .NET 4.0.
I've investigated the .NET custom action. The solution is ok, but I'll have to deploy also the assembly with the custom action and I don't like that.
PowerShell script - not that happy with it because it adds dependency to the PowerShell being deployed on the target machine... not a solution.
Command prompt reg (reg query, reg delete...) - still under investigation...

I feel like I'm spending too much time for what it seems a simple problem.
Can you recommend me solutions to the problem above?

Thanks!
Daniel
Posts: 8283
Joined: Mon Apr 02, 2012 1:11 pm
Contact: Website

Re: Delete registry key if empty on uninstall

Hello and welcome to Advanced Installer forums,

Thank you for your interest in Advanced Installer.
Our software writes the following keys in the registry:
HKLM\Software\<Company_Name>\<Product_Name>\<Component_Name>

Components can be installed separately.
So far, on uninstall of a component, we delete <Component_Name> key, but I want to delete also <Company_Name>\<Product_Name>, if and only if the key is empty.
If the registry key was not affected after installation, windows installer should automatically remove it at uninstall time. This is the default Windows Installer behavior. The Windows Installer will automatically remove registry entries that are authored into the Registry table when the referenced component is removed, or any registry entry created in the process of registering a COM server using the Class table. However, often times there are registry entries that applications create after the application is installed that must be removed, or registry entries created by a custom action during install. Since these registry key/values are not authored into the Installer .msi package, the Windows Installer cannot automatically remove these entries during maintenance mode or during uninstall. In this case, as you already figured out you will need a custom action to remove the related registry entries.
I've investigated the .NET custom action. The solution is ok, but I'll have to deploy also the assembly with the custom action and I don't like that.
PowerShell script - not that happy with it because it adds dependency to the PowerShell being deployed on the target machine... not a solution.
In order to achieve that you can go in the Custom Actions page and add a Launch attached file custom action with sequence after the "Install Executin Stage ->Removed Resources" action group and mark it as deferred in execution time. By using this custom action type the source file (e.g. DLL, EXE. etc) will be embedded in the MSI file but will not be deployed at install time with the other application files.

If you still don't want to use a "Launch attached file" custom action to run your .Net custom action, then as another solution you can use Execute inline script code custom action. Using this custom action you will need only to add your VBS code in "Custom Actions" page of your project (no resource file is needed to be attached or deployed by your installation project).

If you have any question let us know.

All the best,
Daniel
Daniel Radu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
Alin.Sarivan
Posts: 13
Joined: Thu Jul 31, 2014 7:16 am

Re: Delete registry key if empty on uninstall

Hi Daniel,

I've used "Inline script code" custom action with VB script in the end.
It works!

Thanks for your reply/suggestions!
Daniel
Posts: 8283
Joined: Mon Apr 02, 2012 1:11 pm
Contact: Website

Re: Delete registry key if empty on uninstall

You're always welcome.

All the best,
Daniel
Daniel Radu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
Alin.Sarivan
Posts: 13
Joined: Thu Jul 31, 2014 7:16 am

Re: Delete registry key if empty on uninstall

Hi Daniel,

Related to the issue described here I've done some investigation and I have a question.

My setup creates a registry key like: HKLM\Software\<Company_Name>\<Product_Name>\<Component_Name>.
At install, the registry key above has some registry values.
On the <Component_Name> key, in Advanced Installer, in Properties, Operations tab I have checked the "Remove this key with all its values and subkeys" on "Component uninstall and Condition = "REMOVE="ALL" AND NOT UPGRADINGPRODUCTCODE".

Case 1:
- edit the registry values under <Cmponent_Name>
- uninstall the component <Component_Name>
Result - all registry keys are deleted - that's good and this is what I expected to happen

Case 2:
- add new registry values under <Component_Name>
- uninstall the component <Component_Name>
Result - <Component_Name> key is deleted with all its sub key and values, but HKLM\Software\<Company_Name>\<Product_Name> still remains in the registry - not good, I would've expected this key to be deleted.

What I think it happens is:
Step 1: Windows Installer tries to delete the registry key and values that he created (and it's responsible for) and it does that, but it sees that are some registry values that he didn't add so it deletes only the registry values that created and leaves the whole registry key HKLM\Software\<Company_Name>\<Product_Name>\<Component_Name> and the values that didn't create.
Step 2: Advanced Installer then triggers the action that deletes the <Component_Name> and all its subkeys and values, but leaves the HKLM\Software\<Company_Name>\<Product_Name> key

As I said above, I would expect to have HKLM\Software\<Company_Name>\<Product_Name> registry key deleted but it didn't happened because of the action order described above.
If the Advanced Installer delete registry key action is executed after Windows Installer registry clean-up action, I think that this is a bug.

Let me know if what I've described here is what actually happens and if it's a bug and if there's a workaround.

Thanks!
Daniel
Posts: 8283
Joined: Mon Apr 02, 2012 1:11 pm
Contact: Website

Re: Delete registry key if empty on uninstall

Hello,
What I think it happens is:
Step 1: Windows Installer tries to delete the registry key and values that he created (and it's responsible for) and it does that, but it sees that are some registry values that he didn't add so it deletes only the registry values that created and leaves the whole registry key HKLM\Software\<Company_Name>\<Product_Name>\<Component_Name> and the values that didn't create.
Step 2: Advanced Installer then triggers the action that deletes the <Component_Name> and all its subkeys and values, but leaves the HKLM\Software\<Company_Name>\<Product_Name> key

As I said above, I would expect to have HKLM\Software\<Company_Name>\<Product_Name> registry key deleted but it didn't happened because of the action order described above.
If the Advanced Installer delete registry key action is executed after Windows Installer registry clean-up action, I think that this is a bug.
Indeed, your assumption is right, this behavior is caused by an Advanced Installer limitation. The related parent key isn't deleted by Windows Installer because our proprietary remove registry custom action (AI_AiRemoveRegsImmediate) is executed on uninstall after the "RemoveRegistryValues" standard action. Currently, our "AI_AiRemoveRegsImmediate" custom action is executed on the commit mode of the uninstall, while the "RemoveRegistryValues" action is executed on the deferred mode. This is our default implementation and is intended like that because currently we don't have a rollback custom action (for "AI_AiRemoveRegsImmediate" action) to restore the registries removed by the "AI_AiRemoveRegsImmediate" action during rollback. We will add an improvement regarding this in a future version of Advanced Installer. Thank you for bringing this to our attention.

Until then, as a workaround, in order to avoid this behavior you can create your own custom action which will remove the desired registries on uninstall. The custom action should be scheduled as deferred before the "Install Execute Stage-> RemoveRegistryValues" standard action. Also, as another workaround, you can proceed like this:
- go to "Custom Actions" page and add a "Set installer property" custom action with sequence, scheduled as immediate after "Install Execute Stage -> Searches" actions group, like this:
  • Property: DISABLEROLLBACK
    Value: 1
    Execution Stage Condition: only "Uninstall" checked
Please keep in mind that by using the later workaround our "AI_AiRemoveRegsImmediate" will be executed on deferred mode. However, when it will be executed on deferred mode, if a rollback is triggered the removed registries will not be restored. As I said currently this is an Advanced Installer limitation.

If you have any questions let us know.

All the best,
Daniel
Daniel Radu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
Alin.Sarivan
Posts: 13
Joined: Thu Jul 31, 2014 7:16 am

Re: Delete registry key if empty on uninstall

Hi,

I've solved the issue with a VBScript.

Thanks for your feedback!
Daniel
Posts: 8283
Joined: Mon Apr 02, 2012 1:11 pm
Contact: Website

Re: Delete registry key if empty on uninstall

You're welcome. I'm glad you sorted things out.

All the best,
Daniel
Daniel Radu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

Return to “Common Problems”