Skip to content

Instantly share code, notes, and snippets.

@linktohack
Last active March 23, 2023 10:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save linktohack/bd9c3c625b3a05bae429e547e88dba9f to your computer and use it in GitHub Desktop.
Save linktohack/bd9c3c625b3a05bae429e547e88dba9f to your computer and use it in GitHub Desktop.
Remove extranouse org ID
;; evaluate this to remove all IDs that are neither: a target, has attachment(s) nor timestamp (synced with caldav)
;; assume everything is in `~/org', including archived files
;; require `rg', `jq' and `awk'
(let* ((default-directory (expand-file-name "~/org"))
(ids (-> (shell-command-to-string
"rg -a -g '*.org*' --json '^:ID:\s*([A-Za-z00-9\-])+\s*$' | jq -r '.data.submatches | select(.) | .[].match.text' | awk '{ print $2 }' | sort")
(string-trim)
(split-string "\n")))
(targets (-> (shell-command-to-string
(format "rg -a -g '*.org*' --json '\\[id:(%s)]' | jq -r '.data.submatches | select(.) | .[].match.text[4:-1]' | sort" (string-join ids "|")))
(string-trim)
(split-string "\n")))
(not-targets (seq-difference ids targets)))
(save-excursion
(seq-each #'(lambda (id)
(condition-case nil
(progn
(org-id-goto id)
(unless (or (org-entry-get nil "SCHEDULED")
(org-entry-get nil "DEADLINE")
(org-entry-get nil "TIMESTAMP")
(member "ATTACH" (org-get-tags nil t)))
;; (message "DELETE %s" id)
(org-delete-property "ID")))
(error (message "FALSE POSITIVE %s" id))))
not-targets)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment