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

Article translations Article translations
Article ID: 279847
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
Expand all | Collapse all

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:
http://msdn2.microsoft.com/en-us/library/aa394582.aspx


Properties

Article ID: 279847 - Last Review: September 3, 2012 - Revision: 4.0
Keywords: 
kbdswmanage2003swept kbprb KB279847
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com