Microsoft KB Archive/279847

= The RegDelete method of Windows Script Host cannot delete keys that contain subkeys =

Article ID: 279847

Article Last Modified on 12/28/2006

-

APPLIES TO


 * Microsoft Windows Scripting Host 2.5
 * Microsoft Windows Scripting Host 2.5
 * Microsoft Windows Management Instrumentation 1.0
 * Microsoft Windows Management Instrumentation 1.1
 * Microsoft Windows Management Instrumentation 1.2
 * Microsoft Windows Management Instrumentation 1.5

-



This article was previously published under Q279847



Important This article contains information about how to modify the registry. Make sure to back up the registry before you modify it. Make sure that you know how to restore the registry if a problem occurs. For more information about how to back up, restore, and modify the registry, click the following article number to view the article in the Microsoft Knowledge Base:

256986 Description of the Microsoft Windows registry



SYMPTOMS
Windows Script Host (WSH) will return the error message

Unable to remove registry key &quot; &quot;

if an attempt is made to delete a key that contains subkeys.



CAUSE
Windows Script Host does not have the capability to enumerate subkeys of a registry key.



RESOLUTION
One way to work around this problem is to use Windows Management Instrumentation to enumerate a registry key and modify any information necessary.

Another option is to use the ActiveX control RegObj.dll (see &quot;References&quot; for information about downloading this control).



STATUS
This behavior is by design.



MORE INFORMATION
Warning Serious problems might occur if you modify the registry incorrectly by using Registry Editor or by using another method. These problems might require that you reinstall your operating system. Microsoft cannot guarantee that these problems can be solved. Modify the registry at your own risk.

The following code demonstrates how to delete a registry key and all subkeys for that key: Const HKEY_CLASSES_ROOT = &H80000000 Const HKEY_CURRENT_USER = &H80000001 Const HKEY_LOCAL_MACHINE = &H80000002 Const HKEY_USERS        = &H80000003

' Object used to get StdRegProv Namespace Set wmiLocator = CreateObject(&quot;WbemScripting.SWbemLocator&quot;)

' Object used to determine local machine name Set wshNetwork = CreateObject(&quot;WScript.Network&quot;)

' Registry Provider (StdRegProv) lives in root\default namespace. Set wmiNameSpace = wmiLocator.ConnectServer(wshNetwork.ComputerName, &quot;root\default&quot;) Set objRegistry = wmiNameSpace.Get(&quot;StdRegProv&quot;)

' Example Deletion of Value sPath = &quot;SOFTWARE\ABC&quot;

lRC = DeleteRegEntry(HKEY_LOCAL_MACHINE, sPath)

Function DeleteRegEntry(sHive, sEnumPath) ' Attempt to delete key. If it fails, start the subkey ' enumration process. lRC = objRegistry.DeleteKey(sHive, sEnumPath)

' The deletion failed, start deleting subkeys. If (lRC <> 0) Then

' Subkey Enumerator On Error Resume Next

lRC = objRegistry.EnumKey(HKEY_LOCAL_MACHINE, sEnumPath, sNames)

For Each sKeyName In sNames If Err.Number <> 0 Then Exit For lRC = DeleteRegEntry(sHive, sEnumPath & &quot;\&quot; & sKeyName) Next

On Error Goto 0

' At this point we should have looped through all subkeys, trying ' to delete the registry key again. lRC = objRegistry.DeleteKey(sHive, sEnumPath)

End If

End Function

The following sample code demonstrates how to use the ActiveX RebObj object to delete a registry key that has subkeys: Function Reg_DeleteKey(key) 'Deletes key and everything in/below it.

Reg_DeleteKey = False

Dim RegObj, RegKey, ParentKey Set RegObj = CreateObject(&quot;RegObj.Registry&quot;)

If not Reg_ExistKey(key) Then Reg_DeleteKey = True Exit Function End If

On Error Resume Next

Set RegKey = RegObj.RegKeyFromString(key) If err.number = 0 then

'Key exists; get parent. Set ParentKey = RegKey.Parent If err.number = 0 Then 'Got parent. Delete RegKey from Parent.Subkeys. ParentKey.Subkeys.Remove(RegKey.Name) If err.number = 0 Then Reg_DeleteKey = True End If   End if

On Error Goto 0

Set ParentKey = Nothing Set RegKey = Nothing Set RegObj = Nothing End Function

