How to delete a registry key only if it has no values

From NSIS Wiki
Jump to navigationJump to search
Author: lewellyn (talk, contrib)


Description

You may have noticed by now that 'DeleteRegKey /ifempty' considers a key empty if it has no subkeys. But what about when you expect the key to have values but not subkeys?

The Script

      ; Delete Registry Key if there are no more values
      ClearErrors
      EnumRegValue $R0 HKLM "${RegistryKey}" 0
      IfErrors +1 +2
        DetailPrint "(Registry Key Error: ${RegistryKey})" ; Put your error handling here.
      StrCmp $R0 "" +1 +3
        DeleteRegKey HKLM "${RegistryKey}" ; This will delete the key if there are no more values...
        DetailPrint "Removing Registry Key: ${RegistryKey}"
      ;;Continue your stuff here.

When modifying this snippet, please keep in mind how the relative jumps work. The docs are handy with this. :)