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

This article was previously published under Q279847
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.
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
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.
Windows Script Host does not have the capability to enumerate subkeys of a registry key.
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).
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  = &H80000000Const HKEY_CURRENT_USER  = &H80000001Const HKEY_LOCAL_MACHINE = &H80000002Const HKEY_USERS         = &H80000003' Object used to get StdRegProv NamespaceSet wmiLocator = CreateObject("WbemScripting.SWbemLocator")' Object used to determine local machine nameSet 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 ValuesPath = "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 IfEnd 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 = NothingEnd Function				
For more information about WMI, see the following MSDN Web site:


Artikelnummer: 279847 – Letzte Überarbeitung: 09/03/2012 07:30:00 – Revision: 4.0

  • kbdswmanage2003swept kbprb KB279847