# Buggy code
def delete_key_recursive(hive, key, use_32bit_registry=False):
    """
    .. versionadded:: 2015.5.4

    Delete a registry key to include all subkeys.

    :param hive: The name of the hive. Can be one of the following

        - HKEY_LOCAL_MACHINE or HKLM
        - HKEY_CURRENT_USER or HKCU
        - HKEY_USER or HKU

    :param key: The key to remove (looks like a path)

    :param bool use_32bit_registry: Deletes the 32bit portion of the registry on
        64bit installations. On 32bit machines this is ignored.

    :return: A dictionary listing the keys that deleted successfully as well as
        those that failed to delete.
    :rtype: dict

    The following example will remove ``salt`` and all its subkeys from the
    ``SOFTWARE`` key in ``HKEY_LOCAL_MACHINE``:

    CLI Example:

    .. code-block:: bash

        salt '*' reg.delete_key_recursive HKLM SOFTWARE\\salt
    """

    if PY2:
        local_hive = _mbcs_to_unicode(hive)
        local_key = _unicode_to_mbcs(key)
    else:
        local_hive = hive
        local_key = key

    # Instantiate the registry object
    registry = Registry()
    hkey = registry.hkeys[local_hive]
    key_path = local_key
    access_mask = registry.registry_32[use_32bit_registry] | _winreg.KEY_ALL_ACCESS

    if not _key_exists(local_hive, local_key, use_32bit_registry):
        return False

    if (len(key) > 1) and (key.count("\\", 1) < registry.subkey_slash_check[hkey]):
        log.error(
            "Hive:{0} Key:{1}; key is too close to root, not safe to remove".format(
                hive, key
            )
        )
        return False

    # Functions for traversing the registry tree
    def _subkeys(_key):
        """
        Enumerate keys
        """
        i = 0
        while True:
            try:
                subkey = _winreg.EnumKey(_key, i)
                yield subkey
                i += 1
            except WindowsError:  # pylint: disable=E0602
                break

    def _traverse_registry_tree(_hkey, _keypath, _ret, _access_mask):
        """
        Traverse the registry tree i.e. dive into the tree
        """
        _key = _winreg.OpenKey(_hkey, _keypath, 0, _access_mask)
        for subkeyname in _subkeys(_key):
            subkeypath = r"{0}\{1}".format(_keypath, subkeyname)
            _ret = _traverse_registry_tree(_hkey, subkeypath, _ret, access_mask)
            _ret.append("{0}".format(subkeypath))
        return _ret

    # Get a reverse list of registry keys to be deleted
    key_list = []
    key_list = _traverse_registry_tree(hkey, key_path, key_list, access_mask)
    # Add the top level key last, all subkeys must be deleted first
    key_list.append(r"{0}".format(key_path))

    ret = {"Deleted": [], "Failed": []}

    # Delete all sub_keys
    for sub_key_path in key_list:
        try:
            key_handle = _winreg.OpenKey(hkey, sub_key_path, 0, access_mask)
            _winreg.DeleteKey(key_handle, "")
            ret["Deleted"].append(r"{0}\{1}".format(hive, sub_key_path))
        except WindowsError as exc:  # pylint: disable=E0602
            log.error(exc, exc_info=True)
            ret["Failed"].append(r"{0}\{1} {2}".format(hive, sub_key_path, exc))

    broadcast_change()

    return ret


# Traceback (CommandExecutionError)
windows_host:
----------
ID: lgpo_test
Function: lgpo.set
Result: False
Comment: An exception occurred in this state: Traceback (most recent call last):
File "c:\salt\bin\lib\site-packages\salt\state.py", line 1765, in call
**cdata['kwargs'])
File "c:\salt\bin\lib\site-packages\salt\loader.py", line 1705, in wrapper
return f(*args, **kwargs)
File "c:\salt\bin\lib\site-packages\salt\states\win_lgpo.py", line 294, in set_
adml_language=adml_language)
File "c:\salt\bin\lib\site-packages\salt\modules\win_lgpo.py", line 5348, in set_
raise CommandExecutionError(msg.format(regedit))
CommandExecutionError: Error while attempting to set policy Domain controller: Allow server operators to schedule tasks via the registry.  Some changes may not be applied as expected
Started: 15:33:50.693000
Duration: 1578.0 ms
Changes:

Summary for windoze
------------
Succeeded: 0
Failed:    1

# Fixed code
def delete_key_recursive(hive, key, use_32bit_registry=False):
    """
    .. versionadded:: 2015.5.4

    Delete a registry key to include all subkeys.

    :param hive: The name of the hive. Can be one of the following

        - HKEY_LOCAL_MACHINE or HKLM
        - HKEY_CURRENT_USER or HKCU
        - HKEY_USER or HKU

    :param key: The key to remove (looks like a path)

    :param bool use_32bit_registry: Deletes the 32bit portion of the registry on
        64bit installations. On 32bit machines this is ignored.

    :return: A dictionary listing the keys that deleted successfully as well as
        those that failed to delete.
    :rtype: dict

    The following example will remove ``salt`` and all its subkeys from the
    ``SOFTWARE`` key in ``HKEY_LOCAL_MACHINE``:

    CLI Example:

    .. code-block:: bash

        salt '*' reg.delete_key_recursive HKLM SOFTWARE\\salt
    """

    local_hive = _to_unicode(hive)
    local_key = _to_unicode(key)

    # Instantiate the registry object
    registry = Registry()
    hkey = registry.hkeys[local_hive]
    key_path = local_key
    access_mask = registry.registry_32[use_32bit_registry] | win32con.KEY_ALL_ACCESS

    if not _key_exists(local_hive, local_key, use_32bit_registry):
        return False

    if (len(key) > 1) and (key.count("\\", 1) < registry.subkey_slash_check[hkey]):
        log.error(
            "Hive:{0} Key:{1}; key is too close to root, not safe to remove".format(
                hive, key
            )
        )
        return False

    # Functions for traversing the registry tree
    def _subkeys(_key):
        """
        Enumerate keys
        """
        i = 0
        while True:
            try:
                subkey = win32api.RegEnumKey(_key, i)
                yield subkey
                i += 1
            except pywintypes.error:  # pylint: disable=E0602
                break

    def _traverse_registry_tree(_hkey, _keypath, _ret, _access_mask):
        """
        Traverse the registry tree i.e. dive into the tree
        """
        _key = win32api.RegOpenKeyEx(_hkey, _keypath, 0, _access_mask)
        for subkeyname in _subkeys(_key):
            subkeypath = r"{0}\{1}".format(_keypath, subkeyname)
            _ret = _traverse_registry_tree(_hkey, subkeypath, _ret, access_mask)
            _ret.append("{0}".format(subkeypath))
        return _ret

    # Get a reverse list of registry keys to be deleted
    key_list = []
    key_list = _traverse_registry_tree(hkey, key_path, key_list, access_mask)
    # Add the top level key last, all subkeys must be deleted first
    key_list.append(r"{0}".format(key_path))

    ret = {"Deleted": [], "Failed": []}

    # Delete all sub_keys
    for sub_key_path in key_list:
        try:
            key_handle = win32api.RegOpenKeyEx(hkey, sub_key_path, 0, access_mask)
            win32api.RegDeleteKey(key_handle, "")
            ret["Deleted"].append(r"{0}\{1}".format(hive, sub_key_path))
        except WindowsError as exc:  # pylint: disable=E0602
            log.error(exc, exc_info=True)
            ret["Failed"].append(r"{0}\{1} {2}".format(hive, sub_key_path, exc))

    broadcast_change()

    return ret