Automatically add org-files to “org-agenda-files” or remove them, based on whether or not they contain org-keywords

As mentioned before, I use Emacs, org-roam and org-journal. In total I have about 3000 org-files and a tiny fraction of them contains org-keywords. Those files I would like to have in my org-agenda-files (so that they show up in my org-agenda). If there is no TODO or any other org-keyword left, these files should be automatically removed.

This is what I came up with: My solution uses rg (ripgrep) and directly modifies “org-agenda-files”. Put the snippet below in your .emacs, adjust your org-keyword and whenever you save a buffer, “agenda_files.el” is updated. rg is really fast and this causes no tangible delay in my setup.

(defun lt/auto-add-org-agenda ()
  "Automatically add org-files to org-agenda-files or remove them, based on whether or not they contain org-keywords."
  (with-temp-file org-agenda-files (insert (shell-command-to-string (concat "rg --sort modified -e '\\*.(TODO|NEXT|WAITING|RECREATION|PROJECT).[^%]' -g \"*.org\" -l " org-directory)))))
  (add-hook 'org-agenda-mode-hook 'lt/auto-add-org-agenda)