Delete a Registry Key and its Subkeys

A script to delete a registry key and its subkeys via a Custom Action.  Useful for when we want to delete junk registry which has been left behind post uninstall.



Option Explicit

Dim intHive
Dim strComputer
Dim strKeyPath
Dim strSubkey 
Dim arrSubkeys

Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003
Const HKEY_CURRENT_CONFIG = &H80000005

On Error Resume Next 

strComputer = "."
strKeyPath = "Software\Test" 

Dim objRegistry : Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv") 

DeleteSubkeys HKEY_LOCAL_MACHINE, strKeypath 

Set objRegistry  = Nothing

Sub DeleteSubkeys(HKEY_HIVE, strKeyPath) 
    objRegistry.EnumKey HKEY_HIVE, strKeyPath, arrSubkeys 

    If IsArray(arrSubkeys) Then 
        For Each strSubkey In arrSubkeys 
            DeleteSubkeys HKEY_HIVE, strKeyPath & "\" & strSubkey 
    End If 

    objRegistry.DeleteKey HKEY_HIVE, strKeyPath 
End Sub