private ApplyResourceChangeResult <RegistryKeyResource> DoDelete( ApplyResourceChangeInput <RegistryKeyResource> input) { var oldRes = input.PriorState; var pState = StateHelper.Deserialize <PrivateState>(input.PlannedPrivate); _log.LogInformation($"Deleting registry key at [{oldRes.Root}][{oldRes.Path}]"); RegistryKey root = RegUtil.ParseRootKey(oldRes.Root); RegistryKey oldKey = root.OpenSubKey(oldRes.Path, true); if (oldKey != null) { string delKey = null; using (var regKey = oldKey) { ApplyValueDiffs(regKey, toDel: oldRes.Entries); var subKeysLen = oldKey.GetSubKeyNames()?.Length ?? 0; var valuesLen = oldKey.GetValueNames()?.Length ?? 0; var forceDel = oldRes.ForceOnDelete ?? false; if (!(pState?.RegistryKeyCreated ?? false)) { _log.LogWarning("registry key was not created by us"); } if (subKeysLen > 0) { _log.LogWarning($"registry key still has [{subKeysLen}] subkey(s)"); } if (valuesLen > 0) { _log.LogWarning($"registry key still has [{valuesLen}] value(s)"); } if (!(pState?.RegistryKeyCreated ?? false) || subKeysLen > 0 || valuesLen > 0) { if (forceDel) { _log.LogWarning("forced delete specified"); delKey = oldKey.Name; } else { _log.LogWarning("reg key was not created by us or is not empty, SKIPPING delete"); } } else { delKey = oldKey.Name; } } if (delKey != null) { var openParent = RegUtil.OpenParentWithName(delKey, true); if (openParent == null) { _log.LogWarning($"Cannot delete Registry Key [{delKey}], malformed path"); } else { var(delRoot, parent, name) = openParent.Value; _log.LogInformation($"Deleting Registry Key [{name}] under [{(parent??delRoot).Name}] ({delRoot.Name})"); using (var regKey = parent) { regKey.DeleteSubKeyTree(name, false); } } } } else { _log.LogInformation("Could not open existing, prior reg key, skipping"); } return(new ApplyResourceChangeResult <RegistryKeyResource> { NewState = input.PlannedState, Private = null, }); }