The Mastering Emacs ebook cover art

Emacs 28 Edition is out now!

Read a Free Sample

Learn More

Making deleted files go to the trash can

Make Emacs move deleted files to the trash instead of physically deleting them.

In Emacs 23.1 support for your operating system’s trash can (or recycle bin, or whatever) was added. File deletions in Emacs now uses your system’s trash can and the deleted files will be put there instead. The feature must be enabled manually by adding this to your .emacs:

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

The delete to trash functionality will obviously behave differently depending on your operating system. On Windows the special function system-move-file-to-trash is defined because Windows exposes its own API for handling files sent to the recycle bin. On other operating systems that function will be nil, and the default behavior provided by move-file-to-trash is used instead.

In Emacs 23.2 new functionality was added to ensure Emacs conforms to the specification used by all major, free desktop environments. The new variable is trash-directory and determines where Emacs will put the deleted files. If the variable is nil the trash can default is used, otherwise the variable must contain a path string to where the files are to be put.

Further Reading

Have you read my Reading Guide yet? It's a curated guide to most of my articles and I guarantee you'll learn something whether you're a beginner or an expert. And why not check out my book?

Subscribe to the Mastering Emacs newsletter

I write infrequently, so go on — sign up and receive an e-mail when I write new articles