# Org-roam fails with "Selecting deleted buffer" error

**URL:** <https://org-roam.discourse.group/t/org-roam-fails-with-selecting-deleted-buffer-error/3353>\
**Category:** Troubleshooting\
**Created:** [January 31, 2024, 10:05pm UTC](https://org-roam.discourse.group/t/org-roam-fails-with-selecting-deleted-buffer-error/3353 "2024-01-31T22:05:36Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![rhombus](https://avatars.discourse-cdn.com/v4/letter/r/aca169/32.png) [@rhombus](https://org-roam.discourse.group/u/rhombus)\
**Post date:** [January 31, 2024, 10:05pm UTC](https://org-roam.discourse.group/t/org-roam-fails-with-selecting-deleted-buffer-error/3353/1 "2024-01-31T22:05:36Z")

</div>

emacs won’t load my init.el due to an error loading org-roam, though it worked a couple of days ago. I just did a system update, perhaps that’s what triggered it?

```auto
Debugger entered--Lisp error: (error "Selecting deleted buffer")
  set-buffer(#<killed buffer>)
  (save-current-buffer (set-buffer (emacsql-buffer connection)) (and (>= (buffer-size) 2) (string= "#\n" (buffer-substring (- (point-max) 2) (point-max)))))
  (progn (save-current-buffer (set-buffer (emacsql-buffer connection)) (and (>= (buffer-size) 2) (string= "#\n" (buffer-substring (- (point-max) 2) (point-max))))))
  (closure (t) (connection) "Return true if the end of the buffer has a properl..." (progn (save-current-buffer (set-buffer (emacsql-buffer connection)) (and (>= (buffer-size) 2) (string= "#\n" (buffer-substring (- ... 2) (point-max)))))))(#<emacsql-sqlite-connection emacsql-sqlite-connection-1559bf72ba10>)
  apply((closure (t) (connection) "Return true if the end of the buffer has a properl..." (progn (save-current-buffer (set-buffer (emacsql-buffer connection)) (and (>= (buffer-size) 2) (string= "#\n" (buffer-substring (- ... 2) (point-max))))))) #<emacsql-sqlite-connection emacsql-sqlite-connection-1559bf72ba10> nil)
  emacsql-waiting-p(#<emacsql-sqlite-connection emacsql-sqlite-connection-1559bf72ba10>)
  (not (emacsql-waiting-p connection))
  (and (or (null real-timeout) (< (float-time) end)) (not (emacsql-waiting-p connection)))
  (while (and (or (null real-timeout) (< (float-time) end)) (not (emacsql-waiting-p connection))) (let ((saved-match-data (match-data))) (unwind-protect (progn (accept-process-output (emacsql-process connection) real-timeout)) (set-match-data saved-match-data t))))
  (let* ((real-timeout (or timeout emacsql-global-timeout)) (end (if real-timeout (progn (+ (float-time) real-timeout))))) (while (and (or (null real-timeout) (< (float-time) end)) (not (emacsql-waiting-p connection))) (let ((saved-match-data (match-data))) (unwind-protect (progn (accept-process-output (emacsql-process connection) real-timeout)) (set-match-data saved-match-data t)))) (if (emacsql-waiting-p connection) nil (signal 'emacsql-timeout (list "Query timed out" real-timeout))))
  (progn (let* ((real-timeout (or timeout emacsql-global-timeout)) (end (if real-timeout (progn (+ (float-time) real-timeout))))) (while (and (or (null real-timeout) (< (float-time) end)) (not (emacsql-waiting-p connection))) (let ((saved-match-data (match-data))) (unwind-protect (progn (accept-process-output (emacsql-process connection) real-timeout)) (set-match-data saved-match-data t)))) (if (emacsql-waiting-p connection) nil (signal 'emacsql-timeout (list "Query timed out" real-timeout)))))
  (closure (t) (connection &optional timeout) "Block until CONNECTION is waiting for further inpu..." (progn (let* ((real-timeout (or timeout emacsql-global-timeout)) (end (if real-timeout (progn ...)))) (while (and (or (null real-timeout) (< ... end)) (not (emacsql-waiting-p connection))) (let ((saved-match-data ...)) (unwind-protect (progn ...) (set-match-data saved-match-data t)))) (if (emacsql-waiting-p connection) nil (signal 'emacsql-timeout (list "Query timed out" real-timeout))))))(#<emacsql-sqlite-connection emacsql-sqlite-connection-1559bf72ba10>)
  apply((closure (t) (connection &optional timeout) "Block until CONNECTION is waiting for further inpu..." (progn (let* ((real-timeout (or timeout emacsql-global-timeout)) (end (if real-timeout (progn ...)))) (while (and (or (null real-timeout) (< ... end)) (not (emacsql-waiting-p connection))) (let ((saved-match-data ...)) (unwind-protect (progn ...) (set-match-data saved-match-data t)))) (if (emacsql-waiting-p connection) nil (signal 'emacsql-timeout (list "Query timed out" real-timeout)))))) #<emacsql-sqlite-connection emacsql-sqlite-connection-1559bf72ba10> nil)
  emacsql-wait(#<emacsql-sqlite-connection emacsql-sqlite-connection-1559bf72ba10>)
  (let ((sql-string (apply #'emacsql-compile connection sql args))) (emacsql-clear connection) (emacsql-send-message connection sql-string) (emacsql-wait connection) (emacsql-parse connection))
  (progn (let ((sql-string (apply #'emacsql-compile connection sql args))) (emacsql-clear connection) (emacsql-send-message connection sql-string) (emacsql-wait connection) (emacsql-parse connection)))
  (closure (t) (connection sql &rest args) "\n\n(fn CONNECTION SQL &rest ARGS)" (progn (let ((sql-string (apply #'emacsql-compile connection sql args))) (emacsql-clear connection) (emacsql-send-message connection sql-string) (emacsql-wait connection) (emacsql-parse connection))))(#<emacsql-sqlite-connection emacsql-sqlite-connection-1559bf72ba10> [:pragma (= busy-timeout $s1)] 15000)
  apply((closure (t) (connection sql &rest args) "\n\n(fn CONNECTION SQL &rest ARGS)" (progn (let ((sql-string (apply #'emacsql-compile connection sql args))) (emacsql-clear connection) (emacsql-send-message connection sql-string) (emacsql-wait connection) (emacsql-parse connection)))) #<emacsql-sqlite-connection emacsql-sqlite-connection-1559bf72ba10> ([:pragma (= busy-timeout $s1)] 15000))
  emacsql(#<emacsql-sqlite-connection emacsql-sqlite-connection-1559bf72ba10> [:pragma (= busy-timeout $s1)] 15000)
  (let* ((process-connection-type nil) (coding-system-for-write 'utf-8-auto) (coding-system-for-read 'utf-8-auto) (file (slot-value connection 'file)) (buffer (generate-new-buffer " *emacsql-sqlite*")) (fullfile (if file (expand-file-name file) ":memory:")) (process (start-process "emacsql-sqlite" buffer emacsql-sqlite-executable fullfile))) (let* ((v connection)) (eieio-oset v 'process process)) (let* ((v process)) (set-process-sentinel v #'(lambda (proc _) (kill-buffer (process-buffer proc))))) (emacsql-wait connection) (emacsql connection [:pragma (= busy-timeout $s1)] (/ (* emacsql-global-timeout 1000) 2)) (emacsql-register connection))
  (progn (emacsql-sqlite-ensure-binary) (let* ((process-connection-type nil) (coding-system-for-write 'utf-8-auto) (coding-system-for-read 'utf-8-auto) (file (slot-value connection 'file)) (buffer (generate-new-buffer " *emacsql-sqlite*")) (fullfile (if file (expand-file-name file) ":memory:")) (process (start-process "emacsql-sqlite" buffer emacsql-sqlite-executable fullfile))) (let* ((v connection)) (eieio-oset v 'process process)) (let* ((v process)) (set-process-sentinel v #'(lambda (proc _) (kill-buffer (process-buffer proc))))) (emacsql-wait connection) (emacsql connection [:pragma (= busy-timeout $s1)] (/ (* emacsql-global-timeout 1000) 2)) (emacsql-register connection)))
  (progn (progn (emacsql-sqlite-ensure-binary) (let* ((process-connection-type nil) (coding-system-for-write 'utf-8-auto) (coding-system-for-read 'utf-8-auto) (file (slot-value connection 'file)) (buffer (generate-new-buffer " *emacsql-sqlite*")) (fullfile (if file (expand-file-name file) ":memory:")) (process (start-process "emacsql-sqlite" buffer emacsql-sqlite-executable fullfile))) (let* ((v connection)) (eieio-oset v 'process process)) (let* ((v process)) (set-process-sentinel v #'(lambda (proc _) (kill-buffer ...)))) (emacsql-wait connection) (emacsql connection [:pragma (= busy-timeout $s1)] (/ (* emacsql-global-timeout 1000) 2)) (emacsql-register connection))))
  (closure (t) (connection &rest _rest) "\n\n(fn CONNECTION &rest REST)" (progn (progn (emacsql-sqlite-ensure-binary) (let* ((process-connection-type nil) (coding-system-for-write 'utf-8-auto) (coding-system-for-read 'utf-8-auto) (file (slot-value connection ...)) (buffer (generate-new-buffer " *emacsql-sqlite*")) (fullfile (if file ... ":memory:")) (process (start-process "emacsql-sqlite" buffer emacsql-sqlite-executable fullfile))) (let* ((v connection)) (eieio-oset v 'process process)) (let* ((v process)) (set-process-sentinel v #'...)) (emacsql-wait connection) (emacsql connection [:pragma (= busy-timeout $s1)] (/ (* emacsql-global-timeout 1000) 2)) (emacsql-register connection)))))(#<emacsql-sqlite-connection emacsql-sqlite-connection-1559bf72ba10> (:file "~/.emacs.d/org-roam.db"))
  apply((closure (t) (connection &rest _rest) "\n\n(fn CONNECTION &rest REST)" (progn (progn (emacsql-sqlite-ensure-binary) (let* ((process-connection-type nil) (coding-system-for-write 'utf-8-auto) (coding-system-for-read 'utf-8-auto) (file (slot-value connection ...)) (buffer (generate-new-buffer " *emacsql-sqlite*")) (fullfile (if file ... ":memory:")) (process (start-process "emacsql-sqlite" buffer emacsql-sqlite-executable fullfile))) (let* ((v connection)) (eieio-oset v 'process process)) (let* ((v process)) (set-process-sentinel v #'...)) (emacsql-wait connection) (emacsql connection [:pragma (= busy-timeout $s1)] (/ (* emacsql-global-timeout 1000) 2)) (emacsql-register connection))))) (#<emacsql-sqlite-connection emacsql-sqlite-connection-1559bf72ba10> (:file "~/.emacs.d/org-roam.db")))
  #f(compiled-function (&rest args) #<bytecode -0x1a921b7df21097a0>)(#<emacsql-sqlite-connection emacsql-sqlite-connection-1559bf72ba10> (:file "~/.emacs.d/org-roam.db"))
  apply(#f(compiled-function (&rest args) #<bytecode -0x1a921b7df21097a0>) #<emacsql-sqlite-connection emacsql-sqlite-connection-1559bf72ba10> (:file "~/.emacs.d/org-roam.db"))
  initialize-instance(#<emacsql-sqlite-connection emacsql-sqlite-connection-1559bf72ba10> (:file "~/.emacs.d/org-roam.db"))
  #f(compiled-function (class &rest slots) "Default constructor for CLASS `eieio-default-superclass'.\nSLOTS are the initialization slots used by `initialize-instance'.\nThis static method is called when an object is constructed.\nIt allocates the vector used to represent an EIEIO object, and then\ncalls `initialize-instance' on that object." #<bytecode -0xadfb7d5e44f8b22>)(emacsql-sqlite-connection :file "~/.emacs.d/org-roam.db")
  apply(#f(compiled-function (class &rest slots) "Default constructor for CLASS `eieio-default-superclass'.\nSLOTS are the initialization slots used by `initialize-instance'.\nThis static method is called when an object is constructed.\nIt allocates the vector used to represent an EIEIO object, and then\ncalls `initialize-instance' on that object." #<bytecode -0xadfb7d5e44f8b22>) emacsql-sqlite-connection (:file "~/.emacs.d/org-roam.db"))
  make-instance(emacsql-sqlite-connection :file "~/.emacs.d/org-roam.db")
  (let ((connection (make-instance 'emacsql-sqlite-connection :file file))) (if debug (progn (emacsql-enable-debugging connection))) connection)
  (progn (let ((--cl-keys-- --cl-rest--)) (while --cl-keys-- (cond ((memq (car --cl-keys--) '(:debug :allow-other-keys)) (if (cdr --cl-keys--) nil (error "Missing argument for %s" (car --cl-keys--))) (setq --cl-keys-- (cdr (cdr --cl-keys--)))) ((car (cdr (memq ... --cl-rest--))) (setq --cl-keys-- nil)) (t (error "Keyword argument %s not one of (:debug)" (car --cl-keys--)))))) (let ((connection (make-instance 'emacsql-sqlite-connection :file file))) (if debug (progn (emacsql-enable-debugging connection))) connection))
  (let* ((debug (car (cdr (plist-member --cl-rest-- ':debug))))) (progn (let ((--cl-keys-- --cl-rest--)) (while --cl-keys-- (cond ((memq (car --cl-keys--) '...) (if (cdr --cl-keys--) nil (error "Missing argument for %s" ...)) (setq --cl-keys-- (cdr ...))) ((car (cdr ...)) (setq --cl-keys-- nil)) (t (error "Keyword argument %s not one of (:debug)" (car --cl-keys--)))))) (let ((connection (make-instance 'emacsql-sqlite-connection :file file))) (if debug (progn (emacsql-enable-debugging connection))) connection)))
  emacsql-sqlite("~/.emacs.d/org-roam.db")
  org-roam-db()
  org-roam-db-sync()
  org-roam-db-autosync-mode()
  eval-buffer(#<buffer *load*> nil "/home/rhombus/.emacs.d/init.el" nil t) ; Reading at buffer position 2060
  load-with-code-conversion("/home/rhombus/.emacs.d/init.el" "/home/rhombus/.emacs.d/init.el" t t)
  load("/home/rhombus/.emacs.d/init" noerror nomessage)
  startup--load-user-init-file(#f(compiled-function () #<bytecode 0x1eb2de4572631591>) #f(compiled-function () #<bytecode -0x1f3c61addc0b8a75>) t)
  command-line()
  normal-top-level()

```

None of the standard bits of advice I’ve tried have worked:

Deleting org-roam.db and running org-roam-db-sync fails with the same “Selected deleted buffer” error.

I have updated emacsql, no change.

I have manually built emacsql with make, no change.

Does anyone have any other suggestions? Any advice to help me interpret this error message?

---

<div class="post-metadata">

**Author:** ![nobiot](https://yyz2.discourse-cdn.com/free1/user_avatar/org-roam.discourse.group/nobiot/32/159_2.png) [@nobiot](https://org-roam.discourse.group/u/nobiot)\
**Post date:** [January 31, 2024, 10:49pm UTC](https://org-roam.discourse.group/t/org-roam-fails-with-selecting-deleted-buffer-error/3353/2 "2024-01-31T22:49:33Z")

</div>

> [@rhombus](#):
>
> I have updated emacsql, no change

I’d upgrade Emacs to 29.2 and use the bulit-in sqlite option. Have you done this?

---

<div class="post-metadata">

**Author:** ![rhombus](https://avatars.discourse-cdn.com/v4/letter/r/aca169/32.png) [@rhombus](https://org-roam.discourse.group/u/rhombus)\
**Post date:** [January 31, 2024, 11:11pm UTC](https://org-roam.discourse.group/t/org-roam-fails-with-selecting-deleted-buffer-error/3353/3 "2024-01-31T23:11:46Z")

</div>

No, not consciously. I’ll try that at the next opportunity.

---

<div class="post-metadata">

**Author:** ![rhombus](https://avatars.discourse-cdn.com/v4/letter/r/aca169/32.png) [@rhombus](https://org-roam.discourse.group/u/rhombus)\
**Post date:** [February 3, 2024, 11:12am UTC](https://org-roam.discourse.group/t/org-roam-fails-with-selecting-deleted-buffer-error/3353/4 "2024-02-03T11:12:52Z")

</div>

Okay, I’ve installed 29.2 with sqlite, but I’m still getting the same error. Do I need to configure anything to make it work?

---

<div class="post-metadata">

**Author:** ![rhombus](https://avatars.discourse-cdn.com/v4/letter/r/aca169/32.png) [@rhombus](https://org-roam.discourse.group/u/rhombus)\
**Post date:** [February 3, 2024, 11:43am UTC](https://org-roam.discourse.group/t/org-roam-fails-with-selecting-deleted-buffer-error/3353/5 "2024-02-03T11:43:43Z")

</div>

I had an obsolete version of emacsql (3.0.0) installed. I’ve removed it now, but it doesn’t make any difference, the error persists.

I thought perhaps I need to uninstall the emacsql-sqlite 3.1.1 package, but the package manager refuses because it’s an org-roam dependency.

---

<div class="post-metadata">

**Author:** ![rhombus](https://avatars.discourse-cdn.com/v4/letter/r/aca169/32.png) [@rhombus](https://org-roam.discourse.group/u/rhombus)\
**Post date:** [February 3, 2024, 12:14pm UTC](https://org-roam.discourse.group/t/org-roam-fails-with-selecting-deleted-buffer-error/3353/6 "2024-02-03T12:14:16Z")

</div>

Now I’ve tried removing and reinstalling org-roam. It installs emacsql-3.1.1 automatically. Now it compiles the EmacSQL binary upon installing (it did not do that previously, I had to manually build it) but this also made no difference - the error persists.

I don’t understand what is wrong here. If I did, I could investigate more effectively.

---

<div class="post-metadata">

**Author:** ![rhombus](https://avatars.discourse-cdn.com/v4/letter/r/aca169/32.png) [@rhombus](https://org-roam.discourse.group/u/rhombus)\
**Post date:** [February 3, 2024, 12:25pm UTC](https://org-roam.discourse.group/t/org-roam-fails-with-selecting-deleted-buffer-error/3353/7 "2024-02-03T12:25:03Z")

</div>

Apparently there is supposed to be an emacsql-sqlite-builtin package in melpa, but I don’t see it in my package listing:

> **[MELPA](https://melpa.org/#/emacsql-sqlite-builtin)**
>
> The largest and most up-to-date repository of Emacs packages.

It is neither in the full package listing, nor in built-in when I search using paradox.

---

<div class="post-metadata">

**Author:** ![rhombus](https://avatars.discourse-cdn.com/v4/letter/r/aca169/32.png) [@rhombus](https://org-roam.discourse.group/u/rhombus)\
**Post date:** [February 3, 2024, 12:41pm UTC](https://org-roam.discourse.group/t/org-roam-fails-with-selecting-deleted-buffer-error/3353/8 "2024-02-03T12:41:48Z")

</div>

Okay, I think it is in melpa-preview, and I am only attaching melpa-stable.

---

<div class="post-metadata">

**Author:** ![rhombus](https://avatars.discourse-cdn.com/v4/letter/r/aca169/32.png) [@rhombus](https://org-roam.discourse.group/u/rhombus)\
**Post date:** [February 3, 2024, 1:42pm UTC](https://org-roam.discourse.group/t/org-roam-fails-with-selecting-deleted-buffer-error/3353/9 "2024-02-03T13:42:37Z")

</div>

Success!

Background: to use the built-in sqlite in emacs 29.2, you need the emacsql-sqlite-builtin package. This is not pulled automatically by org-roam. The latest stable org-roam (2.2.2) also does not seem to be aware of this.

Solution:

Add the melpa repository with this line in init.el:

```auto
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)

```

Here’s what my packages section looks like now:

```auto

;; Packages
(require 'package)
(add-to-list 'package-archives '("elpa" . "https://elpa.gnu.org/packages/" ) t)
(add-to-list 'package-archives '("melpa-stable" . "http://stable.melpa.org/packages/") t)
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)
(package-initialize)

```

After doing this, you need to reload init.el and then get the updated package list with `M-x package-list-packages`.

Now install org-roam from _melpa_, not melpa-stable. My version is now 20240114.1941.

This will also pull emacsql, but _not_ emacsql-sqlite-builtin. You need this for it to work with the built-in sqlite. Install it. My version is 20240119.2314.

After restarting emacs, the org-roam database is automatically rebuilt, and now it works again!

@nobiot When is the next stable release of org-roam planned? I would prefer to remain on stable releases whenever possible.

---

<div class="post-metadata">

**Author:** ![nobiot](https://yyz2.discourse-cdn.com/free1/user_avatar/org-roam.discourse.group/nobiot/32/159_2.png) [@nobiot](https://org-roam.discourse.group/u/nobiot)\
**Post date:** [February 3, 2024, 5:33pm UTC](https://org-roam.discourse.group/t/org-roam-fails-with-selecting-deleted-buffer-error/3353/10 "2024-02-03T17:33:00Z")

</div>

> [@rhombus](#):
>
> @nobiot When is the next stable release of org-roam planned? I would prefer to remain on stable releases whenever possible.

I do not know. I am not part of the project; just like you. Congratulations on your success 🙂

---

<div class="post-metadata">

**Author:** ![Roger2](https://avatars.discourse-cdn.com/v4/letter/r/da6949/32.png) [@Roger2](https://org-roam.discourse.group/u/Roger2)\
**Post date:** [October 18, 2024, 11:42pm UTC](https://org-roam.discourse.group/t/org-roam-fails-with-selecting-deleted-buffer-error/3353/11 "2024-10-18T23:42:34Z")

</div>

Hey, thanks for submitting your solution. I did exactly the same but I still see the warning each time I open emacs.

This is how my init.el looks like for packages:

```auto
(require 'package)
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)
(add-to-list 'package-archives '("gnu" . "https://elpa.gnu.org/packages/"))
(package-initialize) ;; Initializes the package infrastructure

```

And this is what I added for roam:

```auto
(use-package org-roam
      :ensure t
      :init
      (setq org-roam-v2-ack t)
      :hook
      (after-init . org-roam-mode)
      :custom
      (org-roam-directory "~/xxx/org/roam")
      :bind (:map org-roam-mode-map
              (("C-c n l" . org-roam)
               ("C-c n f" . org-roam-find-file)
               ("C-c n g" . org-roam-graph))
              :map org-mode-map
              (("C-c n i" . org-roam-insert))
              (("C-c n I" . org-roam-insert-immediate))))

```

The first step I did was commenting out the :ensure for the use-package org-roam, then I removed org-roam and resintall it using the packages app. I selected the most recent version, but still the error appeared after the reinstallation.

Thanks!

**Update** : I had to manually uninstall emacsql-sqlite and then reinstall roam … all seems to be working fine now. I wonder if magit is OK. I’ll know next time I need to do a commit 🙂
