# Delete files to Trash on OS X

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
file))


but they don't work:

Trashing...
(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 – user151019 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.

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.