code: def check_completion(task, mark_incomplete=False, clear=False, return_stats=False):
      to_clear = dict()
      is_complete, stats = _check_completion(task,
                                             mark_incomplete=mark_incomplete,
                                             clear=clear,
                                             stats={},
                                             visited=dict(),
                                             to_clear=to_clear)
      while to_clear:
          found_clearable_task = False
          for task_id in list(to_clear.keys()):
              v = to_clear[task_id]
              if not v['required_by']:
                  found_clearable_task = True
                  task = v['task']
                  if isinstance(task, ORMTask):
                      task.mark_incomplete()
                      task.clear()
                      _increment_stats(stats, 'Cleared')
                      config.logger.info("Cleared task: " + task_id)
                  else:
                      config.logger.info('Cannot clear task, not an ORMTask: ' + task_id)
                  del to_clear[task_id]
                  for w in to_clear.values():
                      w['required_by'].discard(task_id)
          if not found_clearable_task:
              raise RuntimeError("Error in recursive task clearing, no clearable task found")
      config.logger.info("Task completion checking, summary:\n" + str(stats))
      if return_stats:
          return is_complete, stats
      else:
          return is_complete