64

To archive the DONE tasks i am using

C-c C-x a

command. The draw back is i have to manually move over the DONE tasks one by one and then archive it.

How to archive all the DONE tasks using a single command.

Talespin_Kit
  • 20,830
  • 29
  • 89
  • 135
  • Just noticed that running `M-x org-archive-subtree C-x-z` (keep pressing z) is close to one command solution. Not sure if that helps anyone :) – bgs Oct 28 '15 at 18:38

8 Answers8

65

You can bulk archive (or refile/change todo etc) from within the Agenda view.

http://orgmode.org/manual/Agenda-commands.html#Agenda-commands

If you call Org-Agenda from within the buffer you want to archive you can temporarily restrict it to only that buffer and view only todo entries and filter for only DONE

C-c a < t
N r

Where N corresponds to the shortcut for your DONE state (with default states it would be 2)

Then you'd simply need to mark all the desired headlines and bulk archive

m (mark for bulk action)
B a (or B $ for arch->sibling)
Jonathan Leech-Pepin
  • 7,684
  • 2
  • 29
  • 45
57

Here's a corrected version of madalu's snippet. Note that this version also only operates on the current subtree (change 'tree back to 'file to operate over the entire file).

(defun org-archive-done-tasks ()
  (interactive)
  (org-map-entries
   (lambda ()
     (org-archive-subtree)
     (setq org-map-continue-from (org-element-property :begin (org-element-at-point))))
   "/DONE" 'tree))
Stefan van der Walt
  • 7,165
  • 1
  • 32
  • 41
31

You can write a function using org-map-entries:

(defun my-org-archive-done-tasks ()
  (interactive)
  (org-map-entries 'org-archive-subtree "/DONE" 'file))
madalu
  • 692
  • 5
  • 4
  • 1
    This is skipping the even sub headings while archiving. Seems like org-map-continue-from variable should be used (i haven't figured out how to use them). Here's the text from help " After evaluation, the cursor will be moved to the end of the line (presumably of the headline of the processed entry) and search continues from there."..."In such cases, you can specify the position from where search should continue by making FUNC set the variable `org-map-continue-from' to the desired buffer position." – Talespin_Kit Dec 04 '11 at 20:37
  • 3
    As @Talespin_Kit has mentioned this answer has a bug, skipping every second DONE entry. [Stefan's answer below](http://stackoverflow.com/a/27043756/244935) fixes it and that one works correctly for me. – Daniel Dinnyes May 10 '15 at 12:15
  • 2
    If this answer has a bug, shouldn't it be unmarked as the accepted answer and have the one that works accepted instead? – Adrian Mar 08 '20 at 15:27
7

Also from http://orgmode.org/manual/Moving-subtrees.html#Moving-subtrees

C-u C-c C-x C-s

Check if any direct children of the current headline could be moved to the archive. To do this, each subtree is checked for open TODO entries. If none are found, the command offers to move it to the archive location. If the cursor is not on a headline when this command is invoked, the level 1 trees will be checked.

Community
  • 1
  • 1
  • it looks like I have to do `C-x h` everytime to select all text then I have to do `C-u C-c C-x C-s` – agent18 Jul 17 '19 at 11:49
2

There is now a command org-archive-all-done that is built into org-mode, and in org-archive.el.

Robert P. Goldman
  • 860
  • 1
  • 8
  • 16
2

Based on Robert's Answer

(require 'org)
(require 'org-archive)
(add-hook 'org-mode-hook (lambda () (add-hook 'after-save-hook 'org-archive-all-done nil t)))
nitin
  • 21
  • 1
  • 2
1

If you want to do it in the source Org buffer (as opposed to in an Org agenda view), and if they are following each other, you can select all of them in a region, and apply a command (such as C-c C-t d).

Only setting needed:

;; Some commands act upon headlines in the active region.
(setq org-loop-over-headlines-in-active-region 'start-level)
fniessen
  • 4,408
  • 19
  • 18
0

I found the direct "org-map-entries" method in a couple of these answers to be a little "fragile" for some reason in situations with more varied nesting and TODOs at multiple levels.

This method - generating a list and then archiving in reverse (to avoid changes in positioning) seems to cover every use case I've thrown at it. Sharing it here for anyone else that runs into trouble.

Note the "TODO" string match on the last line needs to match how you have your TODOs defined exactly (for example in a vanilla case, the match may be: "TODO=\"DONE\"").

(defun org-archive-done-tasks ()
  "Archive all tasks marked DONE in the file."
  (interactive)
  (mapc (lambda(entry)
          (goto-char entry)
          (org-archive-subtree))
        (reverse (org-map-entries (lambda () (point)) "TODO=\"★ DONE\"" 'file))))
MPT
  • 183
  • 8