Sometimes you accidentally evaluate your entire Org-Mode document resulting in result blocks everywhere. Maybe you can’t easily revert the change so you are stuck with a ton of code you don’t need. Here is a function to remove all of your result blocks. It is pretty good for documents that you probably never wanted to evaluate in the first place:
(defconst help/org-special-pre "^\s*#[+]") (defun help/org-2every-src-block (fn) "Visit every Source-Block and evaluate `FN'." (interactive) (save-excursion (goto-char (point-min)) (let ((case-fold-search t)) (while (re-search-forward (concat help/org-special-pre "BEGIN_SRC") nil t) (let ((element (org-element-at-point))) (when (eq (org-element-type element) 'src-block) (funcall fn element))))) (save-buffer))) (define-key org-mode-map (kbd "s-]") (lambda () (interactive) (help/org-2every-src-block 'org-babel-remove-result)))

Hello,
did you know that something similar (or exactly the same is in the core)?
It is org-babel-remove-result-one-or-many
The missing one is removing all results bodies while retaining the result placeholder.
C-u org-babel-remove-result operates only on one block.
I didn’t know that. Thank you for pointing it out.
@not sure org-babel-remove-result-one-or-many removes current or all result blocks in the buffer.
Grant’s solution removes all result blocks after current point.
I needed a similar solution but limited to current or parent subtree so I ended up with:
(defun test/org-subtree-remove-results (&optional up) "Remove result sections in the current subtree. If a numeric prefix UP is given, move up into the hierarchy of headlines by UP levels before removing results the subtree." (interactive "P") (org-with-limited-levels (cond ((org-at-heading-p) (beginning-of-line)) ((org-before-first-heading-p) (user-error "Not in a subtree")) (t (outline-previous-visible-heading 1)))) (when up (while (and (> up 0) (org-up-heading-safe)) (cl-decf up))) (if (called-interactively-p 'any) (call-interactively 'test/org-this-subtree-remove-results) (test/org-this-subtree-remove-results))) (defconst test/org-block-prefix "^\s*#[+]") (defun test/org-this-subtree-remove-results () "Visit every Source-Block and evaluate FN. (interactive) (save-restriction (save-excursion (org-narrow-to-subtree) (goto-char (point-min)) (let ((case-fold-search t)) (while (re-search-forward (concat test/org-block-prefix "BEGIN_SRC") nil t) (let ((element (org-element-at-point))) (when (eq (org-element-type element) 'src-block) (org-babel-remove-result))))) (widen))))Thank you Daniel. FWI I wrapped your code in an HTML pre block.
This looks exactly like what I need, but I’m a fairly new emacs user and I’m confused. I copied the function to my init.el but I don’t know how to invoke it. Thanks.
Mike
Type
and then type
and hit enter.
Hi Grant,
Thanks for this!
I use Spacemacs and wanted to call your function with
M-x org-babel-remove-results, so I added this to mydotspacemacs/user-configfunction in~.spacemacs:;; Remove all #+RESULTS: blocks from org-babel files, adapted from: ;; https://www.wisdomandwonder.com/article/10597/remove-every-source-block-results ;; To use it: ;; M-x org-babel-remove-results (defconst help/org-special-pre "^\s*#[+]") (defun help/org-2every-src-block (fn) "Visit every Source-Block and evaluate `FN'." (interactive) (save-excursion (goto-char (point-min)) (let ((case-fold-search t)) (while (re-search-forward (concat help/org-special-pre "BEGIN_SRC") nil t) (let ((element (org-element-at-point))) (when (eq (org-element-type element) 'src-block) (funcall fn element))))) (save-buffer))) (defun org-babel-remove-results() "Remove all #+RESULTS: blocks from org-babel files" (interactive) (help/org-2every-src-block 'org-babel-remove-result))It seems to work. Hopefully this is helpful for any other Emacs/Spacemacs newbies like me.
Also just to remark that Daniel B commented that the function removes all source blocks “after current point”, but I see it removing them globally in the file. Presumably that’s what
goto-char (point-min)does? I’m not dealing with subtrees in the org file I’m using your code.I realised the function doesn’t remove results from
#+CALL:blocks so I adapted it to do that as well:https://gist.github.com/acbox/2bc3f981cc359736d1c948cccdb098b3
(I wasn’t sure how to format the code in WordPress hence the gist, sorry!)
Than you Daniel.
In WordPress you can wrap code in a
preblock to get the formatting you expect.I just edited your comment to do so.
Presumably that’s what goto-char (point-min) does?
Yes.