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.

1 Answer 1


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
    Commented Feb 26, 2019 at 21:11
  • Ooh, that's a good idea.
    – Willy Lee
    Commented Feb 26, 2019 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. Commented Feb 26, 2019 at 21:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.