Both DeleteFile and DeleteDirectory can only delete files / directories, and lack the option to move them to the trash. Is there an elegant (and OS-independent) way to trash items?

  • 1
    $\begingroup$ It's probably not going to be easy on Windows... JLink/.NETLink etc. might be your best bet $\endgroup$
    – rm -rf
    Aug 3, 2013 at 20:22
  • 2
    $\begingroup$ If you are willing to solve it via Java. You can use FileUtil from incubator project.See the link github.com/gelosie/jdic/blob/master/src/incubator/fileutil/…. There is a method public abstract boolean recycle(File file) under class NativeFileUtil. $\endgroup$
    – s.s.o
    Aug 3, 2013 at 20:52
  • $\begingroup$ Also see here: twall.github.io/jna/3.4.1/javadoc/com/sun/jna/platform/… $\endgroup$
    – rm -rf
    Aug 3, 2013 at 21:13
  • 2
    $\begingroup$ Naked linux does not provide a "trash can", though specific linux window managers might, so it's hard to see how this can be universally OS independent. $\endgroup$ Aug 4, 2013 at 0:10
  • $\begingroup$ Thanks for all the comments! Using a 3rd party Java library or C++ code via JNI isn't exactly elegant, but if its the only way to get the job done, then so be it. I didn't realized that on Linux the trash implementation varies from one distribution to another, so indeed making this fully OS independent seems unlikely to do. $\endgroup$ Aug 4, 2013 at 7:14

1 Answer 1


At first I thought to withhold this answer because it is for OS X only. I thought I would look silly if someone posted a cross-platform solution. But it doesn't look like that's going to happen, so I feel I might as well show how it can be done on an OS that makes it easy. The only tricky bit here is that you have to be aware that the OS X Trash won't accept two files with the same name.

moveToTrash[fullPathName_String] :=
  Module[{trash, fileName},
    trash = FileNameJoin[{HomeDirectory[], ".Trash"}];
    fileName = FileNameTake[fullPathName];
    While[FileExistsQ[FileNameJoin[{trash, fileName}]],fileName = "#" <> fileName];
    RenameFile[fullPathName, FileNameJoin[{trash, fileName}]]]

Can't show any results here; this code is only for its side-effects. However, you can play with using a piece of code that was one of my unit tests. The code should be evaluated a few times, say three, to verify that multiple files with the same name can be moved to Trash.

testFile = 
  Export[FileNameJoin[{HomeDirectory[], "Desktop", "test.txt"}], "The quick brown fox ..."];

Remember this is for OS X only.

  • $\begingroup$ Thanks anyhow! Two small comments though: this moves everything to ~/.Trash, but AFAIK OS X has different trash locations for each volume. So if you trash something that's not on the same volume as ~/, you won't get the native behaviour. Also, how about an $OperatingSystem === "MacOSX" switch? $\endgroup$ Aug 4, 2013 at 18:49
  • $\begingroup$ @TeakeNutma. 1) I happen to prefer that all files get moved to my home .Trash. Since almost every file that goes into any .Trash gets deleted from the disk drive its on, normally it doesn't matter which particular volume it's on. It might matter if I were using portable disk drives, but I don't. 2) Why bother with an OS test when the code is OS X only? My warning is clear. If a Windows user chooses to try it, he/she must accept whatever consequences ensue. $\endgroup$
    – m_goldberg
    Aug 5, 2013 at 2:07

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.