Is there a best, most future-proof method of setting emacs on OS X to delete files to the trash?

I tried the instructions here:

(setq delete-by-moving-to-trash t)

(defun system-move-file-to-trash (file)
  "Use \"trash\" to move FILE to the system trash.
When using Homebrew, install it using \"brew install trash\"."
  (call-process (executable-find "trash")
    nil 0 nil

but they don't work:

(wrong-type-argument stringp nil)

Also, it feels wrong to have to install a separate program in order for Emacs to be able to trash files. Furthermore, I won't remember to install the "trash" program the next time I do a clean OS X install.

Update: I added this to my .emacs and removed the call to trash and it seems to work. I wonder why Emacs on OSX defaults to the FreeDesktop ~/.local/share/Trash location.

(setq trash-directory "~/.Trash")

The following is required:

(setq delete-by-moving-to-trash t)
(setq trash-directory "~/.Trash")

The function move-file-to-trash has three tests: (1) whether trash-directory is defined; (2) whether (fboundp 'system-move-file-to-trash); and, (3) (t . . . the catch-all that uses ~/.local/share... The second test always fails on OSX 10.6.8 when built with the standard option of --with-ns. I don't have access at the moment to other versions of OSX.

  • However OS X al tracks where the file came from if you use trade from the finder and that does not occur here - so note this is not a full implementation but often good enough – mmmmmm Oct 19 '15 at 21:57

Define the function system-move-file-to-trash in this way should be enough:

(defun system-move-file-to-trash (file)
  (call-process "trash" nil nil nil file))

if you don't have trash installed, Emacs will tell you. then, don't set the variable trash-directory since trash does that for you.

About Emacs's trash behavior:

move-file-to-trash is an interactive compiled Lisp function in ‘files.el’.

(move-file-to-trash FILENAME)

Move the file (or directory) named FILENAME to the trash. When ‘delete-by-moving-to-trash’ is non-nil, this function is called by ‘delete-file’ and ‘delete-directory’ instead of deleting files outright.

If the function ‘system-move-file-to-trash’ is defined, call it with FILENAME as an argument. Otherwise, if ‘trash-directory’ is non-nil, move FILENAME to that directory. Otherwise, trash FILENAME using the freedesktop.org conventions, like the GNOME, KDE and XFCE desktop environments. Emacs only moves files to "home trash", ignoring per-volume trashcans.

I guess (setq trash-directory "~/.Trash") is not working: Finder will not be able to recover these files since it (at least) doesn't know where these files come from.


I've been using the osx-trash package. It works with or without the trash command line utility, and integrates properly with the macOS Trash directory.

  • Without the trash command line utility you can ear the trash sound. With the trash command line utility it's faster. – Raoul HATTERER Apr 26 '20 at 9:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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