Fixing TRegistry.DeleteKey

Microsoft decided to make the registry API work differently on Win9X to WinNT/2000. How nice !

Fixing DeleteKey

From the WinAPI help on DeleteKey....

Call DeleteKey to remove a specified key and its associated data,

if any, from the registry. Under Windows 95, if the key has subkeys,

the subkeys and any associated data are also removed.

Under Windows NT, subkeys must be explicitly deleted by separate

calls to DeleteKey.

...So, code that you write on Win9x that works (deletes the key

regardless of subkeys) will not work on WinNT/2000.

So I found it necessary to write my own implementation which

recursively calls iteself in order to delete sub-keys.

procedure DeleteRegKey(aRoot : HKey; aPath : String);


  SL : TStringList;

  X : Integer;


  SL := TStringList.Create;

  with TRegistry.Create do


    RootKey := aRoot;

    if OpenKey(aPath,False) then begin


      For X:=0 to SL.Count-1 do DeleteRegKey(aRoot,aPath + '\' + SL[X]);









TIP:To understand recursion, you must first understand recursion!


Share this article!

Follow us!

Find more helpful articles: