Any recommended methodology to delete an org-roam file?

Is there any reason why I shouldn’t just delete an org-roam file? Any risk to database/links etc, is what I’m thinking about. (Sorry if this is too trivial).

1 Like

Right now, deleting a file will result in links to the current file breaking.

If you delete the file using Emacs (with dired, or calling M-x delete-file), the cache remains consistent. If you delete the file from outside Emacs, it won’t, but is not a big issue, because running M-x org-roam-db-build-cache once more will bring it to a consistent state.

2 Likes

Thank you, that’s very helpful.

I use crux-delete-file-and-buffer https://github.com/bbatsov/crux
to delete the file of the current buffer.
That’s a few steps less then delete-file
and the cache seems to be updated the same. It probably calls delete-file anyway.

1 Like

Any updates on the best way to delete a file?

1 Like

I currently handle that with a script to remove all the references first, then I delete the file:

#!/usr/bin/env python3

import re
import os
import subprocess
import sys

def get_id(fname):
    with open(fname) as f:
        content = f.read()
        return re.findall(r":ID:\s+(.*)", content)[0]

def notes_with_pattern(pat):
    roam_folder = os.path.expanduser("~/.roam/")
    raw = subprocess.check_output(f"rg -l {pat} {roam_folder}", shell=True).decode("utf-8")
    return raw.splitlines()

def subid(content, ident):
    pat = f"\[\[id:{ident}\]\[(.*?)\]\]"
    return re.sub(pat, "\\1", content)

def remove_note_mention(fn, ident):
    with open(fn) as f:
        content = f.read()
    newcontent = subid(content, ident)
    with open(fn, "w") as f:
        f.write(newcontent)
    print(f"Edited {fn}")

def delete_note(fname):
    ident = get_id(fname)
    print(f"Removing id {ident}")
    to_edit = [n for n in notes_with_pattern(ident)
               if n != fname]
    for fn in to_edit:
        remove_note_mention(fn, ident)

if __name__ == '__main__':
    delete_note(sys.argv[1])

And I call it from emacs with:

(defun delete-org-roam-note-references ()
  (interactive)
  (async-shell-command (s-concat "delete_note.py " (buffer-file-name))))

Note: It does not work with heading references just whole notes ones.