# Any recommended methodology to delete an org-roam file?

**URL:** https://org-roam.discourse.group/t/any-recommended-methodology-to-delete-an-org-roam-file/54
**Category:** How To
**Created:** [May 5, 2020, 4:22am UTC](https://org-roam.discourse.group/t/any-recommended-methodology-to-delete-an-org-roam-file/54 "2020-05-05T04:22:04Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![sustainednz](https://yyz2.discourse-cdn.com/free1/user_avatar/org-roam.discourse.group/sustainednz/32/15_2.png) [@sustainednz](https://org-roam.discourse.group/u/sustainednz)
#### Post date: [May 5, 2020, 4:22am UTC](https://org-roam.discourse.group/t/any-recommended-methodology-to-delete-an-org-roam-file/54/1 "2020-05-05T04:22:04Z")

</div>

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).

---

<div class="post-metadata">

### Author: ![jethro](https://avatars.discourse-cdn.com/v4/letter/j/b5a626/32.png) [@jethro](https://org-roam.discourse.group/u/jethro)
#### Post date: [May 5, 2020, 6:05am UTC](https://org-roam.discourse.group/t/any-recommended-methodology-to-delete-an-org-roam-file/54/2 "2020-05-05T06:05:45Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![sustainednz](https://yyz2.discourse-cdn.com/free1/user_avatar/org-roam.discourse.group/sustainednz/32/15_2.png) [@sustainednz](https://org-roam.discourse.group/u/sustainednz)
#### Post date: [May 5, 2020, 6:56am UTC](https://org-roam.discourse.group/t/any-recommended-methodology-to-delete-an-org-roam-file/54/3 "2020-05-05T06:56:32Z")

</div>

Thank you, that’s very helpful.

---

<div class="post-metadata">

### Author: ![zoechi](https://yyz2.discourse-cdn.com/free1/user_avatar/org-roam.discourse.group/zoechi/32/75_2.png) [@zoechi](https://org-roam.discourse.group/u/zoechi)
#### Post date: [May 15, 2020, 5:50am UTC](https://org-roam.discourse.group/t/any-recommended-methodology-to-delete-an-org-roam-file/54/4 "2020-05-15T05:50:29Z")

</div>

I use `crux-delete-file-and-buffer` [https://github.com/bbatsov/crux](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.

---

<div class="post-metadata">

### Author: ![ethan](https://yyz2.discourse-cdn.com/free1/user_avatar/org-roam.discourse.group/ethan/32/307_2.png) [@ethan](https://org-roam.discourse.group/u/ethan)
#### Post date: [August 18, 2020, 5:12am UTC](https://org-roam.discourse.group/t/any-recommended-methodology-to-delete-an-org-roam-file/54/5 "2020-08-18T05:12:05Z")

</div>

Any updates on the best way to delete a file?

---

<div class="post-metadata">

### Author: ![laurent](https://yyz2.discourse-cdn.com/free1/user_avatar/org-roam.discourse.group/laurent/32/1100_2.png) [@laurent](https://org-roam.discourse.group/u/laurent)
#### Post date: [February 11, 2022, 8:12pm UTC](https://org-roam.discourse.group/t/any-recommended-methodology-to-delete-an-org-roam-file/54/6 "2022-02-11T20:12:51Z")

</div>

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

```python
#!/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:

```auto
(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.
