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 “~/Documents/GitHub/org/dotfiles/agenda_files.el” is my “org-agenda-files”-file (replace this with yours). Put the snippet below in your .emacs 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."
  (interactive)
  (with-temp-file org-agenda-files (insert (shell-command-to-string (concat "rg -e '\\*.(TODO|NEXT|WAITING|RECREATION|PROJECT).[^%]' -g \"*.org\" -l " org-directory)))))
  (add-hook 'org-agenda-mode-hook 'lt/auto-add-org-agenda)

2023_02_21-1211: Fixed a stupid typo. 2023_02_24-1445: Corrected add-hook. 2023_02_27-1926: Found a much better hook. Now org-agenda-files is only updated right before the agenda is built. 2023_03_01-0825: If you are working with org-notifications you have to replace org-agenda-files (in the line starting with “(with-temp-file… )” ) with the file specified by org-agenda-files.