code: def _unlock_or_remove(dset_path, paths, remove=False):
      """Unlock `paths` if content is present; remove otherwise.
  
      Parameters
      ----------
      dset_path : str
      paths : list of string
          Absolute paths of dataset files.
      remove : bool, optional
          If enabled, always remove instead of performing an availability test.
  
      Returns
      -------
      Generator with result records.
      """
      dset_path = _dset_arg_kludge(dset_path)
  
      existing = []
      for path in paths:
          if op.exists(path) or op.lexists(path):
              existing.append(path)
          else:
              # Avoid unlock's warning because output files may not exist in
              # common cases (e.g., when rerunning with --onto).
              lgr.debug("Filtered out non-existing path: %s", path)
  
      if not existing:
          return
  
      to_remove = []
      if remove:
          # when we force-remove, we use status to discover matching content
          # and let unlock's remove fallback handle these results
          to_remove = Status()(
              dataset=dset_path,
              path=existing,
              eval_subdataset_state="commit",
              untracked="no",
              annex="no",
              on_failure="ignore",
              # no rendering here, the relevant results are yielded below
              result_renderer="disabled",
              return_type="generator",
              # we only remove files, no subdatasets or directories
              result_filter=lambda x: x.get("type")
              in ("file", "symlink"),
          )
      else:
          # Note: If Unlock() is given a directory (including a subdataset)
          # as a path, files without content present won't be reported, so
          # those cases aren't being covered by the "remove if not present"
          # logic below.
          for res in Unlock()(
              dataset=dset_path,
              path=existing,
              on_failure="ignore",
              result_renderer="disabled",
              return_type="generator",
          ):
              if (
                  res["status"] == "impossible"
                  and res["type"] == "file"
                  and "cannot unlock" in res["message"]
              ):
                  to_remove.append(res)
                  continue
              yield res
      # Avoid `datalad remove` because it calls git-rm underneath, which will
      # remove leading directories if no other files remain. See gh-5486.
      for res in to_remove:
          try:
              os.unlink(res["path"])
          except OSError as exc:
              ce = CapturedException(exc)
              yield dict(
                  res,
                  action="run.remove",
                  status="error",
                  message=("Removing file failed: %s", ce),
                  exception=ce,
              )
          else:
              yield dict(
                  res,
                  action="run.remove",
                  status="ok",
                  message="Removed file",
              )
  
imports: import json
  import logging
  import os
  import os.path
  import warnings
  from argparse import REMAINDER
  from pathlib import Path
  from tempfile import mkdtemp
  import datalad
  import datalad.support.ansi_colors
  from datalad.config import anything2bool
  from datalad.core.local.save import Save
  from datalad.core.local.status import Status
  from datalad.distribution.dataset import Dataset, EnsureDataset, datasetmethod, require_dataset
  from datalad.distribution.get import Get
  from datalad.distribution.install import Install
  from datalad.interface.base import Interface, build_doc, eval_results
  from datalad.interface.common_opts import jobs_opt, save_message_opt
  from datalad.interface.results import get_status_dict
  from datalad.interface.utils import generic_result_renderer
  from datalad.local.unlock import Unlock
  from datalad.support.constraints import EnsureBool, EnsureChoice, EnsureNone
  from datalad.support.exceptions import CapturedException, CommandError
  from datalad.support.globbedpaths import GlobbedPaths
  from datalad.support.json_py import dump2stream
  from datalad.support.param import Parameter
  from datalad.ui import ui
  from datalad.utils import SequenceFormatter, chpwd, ensure_list, ensure_unicode, get_dataset_root, getpwd, join_cmdline, quote_cmdlinearg
label_desc: Use image node_id 8 for calling a global function with example usage: _unlock_or_remove(dset_path, paths, remove) without return types