public void deleteTask(TaskEntity task, String deleteReason, boolean cascade) {
if (!task.isDeleted()) {
task.setDeleted(true);
CommandContext commandContext = Context.getCommandContext();
String taskId = task.getId();
List<Task> subTasks = findTasksByParentTaskId(taskId);
for (Task subTask: subTasks) {
((TaskEntity) subTask).delete(deleteReason, cascade);
}
commandContext
.getIdentityLinkManager()
.deleteIdentityLinksByTaskId(taskId);
commandContext
.getVariableInstanceManager()
.deleteVariableInstanceByTask(task);
if (cascade) {
commandContext
.getHistoricTaskInstanceManager()
.deleteHistoricTaskInstanceById(taskId);
} else {
commandContext
.getHistoricTaskInstanceManager()
.markTaskInstanceEnded(taskId, deleteReason);
if (TaskEntity.DELETE_REASON_COMPLETED.equals(deleteReason)) {
task.createHistoricTaskDetails(UserOperationLogEntry.OPERATION_TYPE_COMPLETE);
} else {