I tend not to delete buffers in emacs, so over time, the number of buffers tends to grow to a hundred or more. (At the moment, *Buffer List* has 144 lines.)

Many of these files are java files, and with my configuration, loading a Java file takes a second or two. This means that starting emacs can take a few minutes.

Is there a way to limit the number of buffers that are saved by the desktop? Ideally, I would like to drop buffers that I have not visited in a while.

I tried this:

(advice-add 'no-save-unseen-buffers :before-while 'desktop-save-buffer-p
              (lambda (buffer &optional rest)
                (let ((last-time (with-current-buffer buffer buffer-display-time)))
                  (when (and last-time
                             (< (time-to-seconds (time-subtract (current-time) last-time))
                                (* 2 7 24 60 60)))))))

but it does not seem to have an effect.

Advice on how to make the above code work, a completely different approach or package that does what I want are all fine answers.


You can try https://www.emacswiki.org/emacs/MidnightMode -- by default this will cleanup old buffers once a night.

To reduce the delay time on opening emacs with a lot of saved desktop buffers, you can modify desktop-restore-eager - set it to a low number and only those will be opened immediately, with the rest being opened lazily.

  • I would expect you can also combine desktop and midnight more explicitly with (add-hook 'desktop-save-hook 'clean-buffer-list) – phils Feb 26 '19 at 21:11
  • Ooh, that's a good idea. – Willy Lee Feb 26 '19 at 21:40
  • The midnight mode page also says that, as of about 10 years ago, desktop does not preserve buffer-display-time, which would explain why my code did not usually work. – Troy Daniels Feb 26 '19 at 21:53

Your Answer

