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'."
  (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)))

6 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")
      (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))))
    

Leave a Reply

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