The eln-cache directory stores the native-compiled *.eln files for your configuration's *.el files.
This feature was added in Emacs 28.1, and is optional at build-time. It is not enabled by default, so you have either installed a version of Emacs which someone else built with this feature enabled, or else you have compiled Emacs yourself with this feature enabled.
If you install a version of Emacs which was not built --with-native-compilation then no *.eln files will be generated anywhere.
(Update: Starting from Emacs 30, native-compilation is enabled by default as a build option, and so you should explicitly ./configure --with-native-compilation=no when building Emacs if you wish to disable this.)
If you still want native-compilation, then I suggest something along the lines of:
- Exit Emacs
mv ~/.emacs.d/eln-cache ~/.eln-cache ln -s ../.eln-cache ~/.emacs.d/eln-cache - Start Emacs
Assuming that when you synchronise your config to another machine the symlink gets copied as a symlink.
You would need to create ~/.eln-cache on each of your machines, of course. (I'm not actually sure what Emacs will do in that situation if eln-cache is a broken symlink.)
Starting from Emacs 29, you can alternatively call the following function in your early-init.el file to move the cache outside of the directory that you are synchronising:
(defun startup-redirect-eln-cache (cache-directory) "Redirect the user's eln-cache directory to CACHE-DIRECTORY. CACHE-DIRECTORY must be a single directory, a string. This function destructively changes `native-comp-eln-load-path' so that its first element is CACHE-DIRECTORY. If CACHE-DIRECTORY is not an absolute file name, it is interpreted relative to `user-emacs-directory'. For best results, call this function in your early-init file, so that the rest of initialization and package loading uses the updated value." ;; Remove the original eln-cache. (setq native-comp-eln-load-path (cdr native-comp-eln-load-path)) ;; Add the new eln-cache. (push (expand-file-name (file-name-as-directory cache-directory) user-emacs-directory) native-comp-eln-load-path))
I've included the definition in full in case you want to experiment with that in Emacs 28. (I haven't tried doing that, so I have no idea whether or not it would work.)