Apple - Is there a way to prevent those pesky .DS_Store files from being ever created?

To prevent those ugly .DS_Store files from showing up there seems to be a relatively new option to do this cleanly, not only for network drives, like:

defaults write DSDontWriteNetworkStores -bool true

but also for USB-connected volumes:

defaults write DSDontWriteUSBStores -bool true

Relative to the OPs question title, this is the best solution without hacks, since it addresses not only the network misbehaviour but is also effected for most connected disks. And those might be the biggest annoyance for most users sharing devices like USB sticks between different environments. It uses only Apple provided options and doesn't interfere with normal operation in any way.

Relative to the concrete situation explained in the OPs body it is of limited value, since this USB option is only available in later versions of OS X/macOS. Further, this still does not address the problem of .DS_Store creation on internal disks, where they can be also a hassle to deal with. Although in that place they might actually serve some kind of purpose.

A more comprehensive solution to this problem, albeit a more hackish one, that works on most versions of the Mac operating system is as follows: To completely nuke this nasty behaviour on all disks – and with a binary patcher/code injector like the question of the OP longs for – there is DeathToDSStore / with source.

Both of these options assume that anyone still uses Finder… Using a different file manager would also solve this problem. There are numerous contenders to replace Finder for file management on macOS. Two of the top dogs in this field are Pathfinder or XFile.

I haven't found any way to keep them from being created, but you can delete them automatically with a script:


find ~ -name .DS_Store -exec /bin/rm -f -- {} \;

exit 0

Run it every X minutes via launchd or cron. It takes almost no time to run on my several-year-old iMac. It will only search your $HOME (~) but that's where most of them will be. Change ~ to / if you want to delete them across the entire drive, but that will take much longer to run, so adjust the frequency accordingly.

Try this command: defaults write DSDontWriteNetworkStores true in terminal. It seems to work for me.