2 out of 2 rated this helpful - Rate this topic

SHDeleteKey function

Applies to: desktop apps only

Deletes a subkey and all its descendants. The function will remove the key and all of the key's values from the registry.

Syntax

LSTATUS SHDeleteKey(
  __in      HKEY hkey,
  __in_opt  LPCTSTR pszSubKey
);

Parameters

hkey [in]

Type: HKEY

A handle to the currently open key, or any of the following predefined values.

HKEY_CLASSES_ROOT

HKEY_CURRENT_CONFIG

HKEY_CURRENT_USER

HKEY_LOCAL_MACHINE

HKEY_PERFORMANCE_DATA

HKEY_USERS

pszSubKey [in, optional]

Type: LPCTSTR

The address of a null-terminated string specifying the name of the key to delete.

Return value

Type: LSTATUS

Returns ERROR_SUCCESS if successful, or a nonzero error code defined in Winerror.h otherwise. You can use the FormatMessage function with the FORMAT_MESSAGE_FROM_SYSTEM flag to retrieve a generic description of the error.

Requirements

Minimum supported client

Windows 2000 Professional, Windows XP

Minimum supported server

Windows 2000 Server

Header

Shlwapi.h

Library

Shlwapi.lib

DLL

Shlwapi.dll (version 4.71 or later)

Unicode and ANSI names

SHDeleteKeyW (Unicode) and SHDeleteKeyA (ANSI)

 

 

Send comments about this topic to Microsoft

Build date: 3/7/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Does not delete 64-bit key from 32-bit process on Windows XP X64
On XP x64, ShDelete tries to access the 32-bit registry view even though the handle passed to it was opened with RegOpenKeyEx and KEY_WOW64_64KEY.  The same code works properly on Win7 x64.

Does not delete 64-bit key from 32-bit process on Windows XP X64
On XP x64, ShDelete tries to access the 32-bit registry view even though the handle passed to it was opened with RegOpenKeyEx and KEY_WOW64_64KEY.  The same code works properly on Win7 x64.

Function no longer works from Windows 7 to XP
This function, as well as RegDeleteTree will now fail if called from a Windows 7 client to remotely delete a key on a Windows XP computer.  Problem was reported to Microsoft, they know about it, but indicated a fix is unlikely.    The only method to delete a key on Windows XP (when executed on Windows 7) is to recursively call RegDeleteKey.
Deleting hkey itself
If pszSubKey is an empty string (but not NULL) then hkey itself is deleted. hkey will not be closed in such case but any operation on it (other then closing it) will fail with error code ERROR_KEY_DELETED (value 1018).
use RegDeleteTree() on Vista or above instead

on Vista this functionality is supported in the base OS via RegDeleteTree()

http://msdn.microsoft.com/en-us/library/aa379776(VS.85).aspx

Note that RegDeleteKey() does not recurse and will fail on keys with subkeys