The RegDelete method of Windows Script Host cannot delete keys that contain subkeys (279847)



The information in this article applies to:

  • Microsoft Windows Script Host 1.0
  • Microsoft Windows Script Host 2.0
  • 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 "<path to registry key>"
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 "References" 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("WbemScripting.SWbemLocator")

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

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

' Example Deletion of Value
sPath = "SOFTWARE\ABC"

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 & "\" & 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("RegObj.Registry")

	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
				

REFERENCES

For more information about WMI, see the following MSDN Web site: You can download RegObj.dll from the following Microsoft Web site:

Modification Type:MajorLast Reviewed:5/2/2006
Keywords:kbDSWManage2003Swept kbprb KB279847