Remove Every Source Block Results

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'."
    (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)))))
(define-key org-mode-map (kbd "s-]") (lambda () (interactive)

8 thoughts on “Remove Every Source Block Results”

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

  2. @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")
       (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)
    (defconst test/org-block-prefix "^\s*#[+]")
    (defun test/org-this-subtree-remove-results ()
      "Visit every Source-Block and evaluate FN.
          (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)
  3. 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.


Leave a Reply

Your email address will not be published. Required fields are marked *