Skip to content

Registry Delete Key

A minimal C program that opens a registry key and deletes a subkey with RegDeleteKey.

Published Mar 31, 1996

The three-call sequence for removing a registry subkey: open the parent with RegOpenKeyEx, delete the child by name with RegDeleteKey, and close the handle. It is the shortest possible reference for cleanup code - uninstallers, test harnesses that need to reset state, tools that remove stale entries. Note that RegDeleteKey only removes empty subkeys; if the target has values or children of its own you must clear those first (or use RegDeleteTree on Vista+).

#include <windows.h>
#include <stdio.h>

//////////////////////////////////////////////////////////////////////

int main()
{
    HKEY key;
    RegOpenKeyEx(HKEY_CLASSES_ROOT, "*", 0, 0, &key) ;
    RegDeleteKey ( key, "123" );
    RegCloseKey ( key );
    printf("Registry Key Was Successfully Removed!");
    return 0;
}

In production code check the return value of each call - this snippet assumes success - and prefer RegDeleteTree when you genuinely want the whole subtree gone in one call.

An unhandled error has occurred. Reload 🗙