I frequently run shell sessions under Emacs (via C-x shell). My practice is to turn auto-saving on these shell-interaction buffers, so that the resulting (auto-saved) files serve as a persistent record of what I've done (including all the generated output).

This works pretty well most of the time, but occasionally, Emacs deletes the auto-saved file.

The conditions that lead to such deletions are not entirely clear to me, but in any case, I would like to prevent them altogether.

Is there a way to tell Emacs to never delete an auto-saved file?

I realize that here I am probably using Emacs' auto-save feature in a way that is very far from the standard use-case for this feature, and therefore, it may be unreasonable to try to do what I am asking about here (i.e. keeping Emacs from deleting an auto-saved file). If so, is there some other way to have Emacs periodically save a shell-interaction buffer to disk, and never deleting the target file?

2 Answers 2


Have you tried customizing option delete-auto-save-files to nil?

delete-auto-save-files is a variable defined in files.el.

Its value is t


Non-nil means delete auto-save file when a buffer is saved or killed.

Note that the auto-save file will not be deleted if the buffer is killed when it has unsaved changes.

You can customize this variable.

See also:

  • 1
    Thank you for this idea! True, I am reluctant to use it, because it would result in the proliferation of other auto-saved files that I am not interested in preserving. BUT, by searching the source code where delete-auto-save-files is used, I discovered another, more targeted, way to prevent Emacs from deleting an auto-save'd files. I've described this alternative in an answer to my own question.
    – kjo
    Commented Jan 20, 2023 at 22:27

At least as far as my installation of Emacs (v. 28.0.50) goes, below is the function responsible for deleting auto-saved files:

(defun delete-auto-save-file-if-necessary (&optional force)
  "Delete auto-save file for current buffer if `delete-auto-save-files' is t.
Normally delete only if the file was written by this Emacs since
the last real save, but optional arg FORCE non-nil means delete anyway."
  (and buffer-auto-save-file-name delete-auto-save-files
       (not (string= buffer-file-name buffer-auto-save-file-name))
       (or force (recent-auto-save-p))
     (condition-case ()
         (delete-file buffer-auto-save-file-name)
       (file-error nil))

Therefore, it appears that it suffices to make the value of the buffer's buffer-file-name variable match that of its buffer-auto-save-file-name variable to keep Emacs from deleting the buffer's auto-saved file.

Note that while setting delete-auto-save-files to nil would cause all auto-save'd files to be preserved, making a buffer's buffer-file-name and buffer-auto-save-file-name variables match disables the deletion of only that buffer's auto-save'd file.

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.