January 1st, 2025

How is it possible to get ERROR_KEY_DELETED when I’m creating a key?

Some time ago, I explained that the ERROR_KEY_DELETED error code means that you tried to perform an operation on a deleted key. But what does it mean when you get the error when trying to create a key?

The key it’s complaining about is not the key you’re trying to create. The problem is with the key that you are trying to create the key beneath.

auto err = RegCreateKeyEx(parentKey, subkeyName,
    0, nullptr, REG_OPTION_NON_VOLATILE, KEY_WRITE, nullptr, &childKey);

If the parent key is deleted, then any future attempt to use that parent key (including creating child keys under it) will return ERROR_KEY_DELETED.

We’ll look into this some more next time.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

1 comment

  • Adam Rosenfield 1 day ago

    The same thing can happen with creating files or directories: if any parent directory in the path being created doesn't exist, then the syscall will fail with ENOENT (POSIX) / ERROR_PATH_NOT_FOUND (Windows). Windows at least has the two separate error codes ERROR_PATH_NOT_FOUND and ERROR_FILE_NOT_FOUND, so users can distinguish between parent directory not found vs. the leaf file not being found (for non-creating open dispositions), whereas POSIX makes no such distinction.

    Read more