How to Stop Ubuntu from Automatically Deleting Temporary Files

The Silent Cleanup

Ubuntu (like most Linux systems) utilizes a specific directory, /tmp, designed exclusively for storing temporary files created by running applications. These files are essential while a program is active but become useless junk once the program closes. By default, Ubuntu relies on a combination of systemd timers (specifically systemd-tmpfiles-clean.timer) and occasionally the older tmpreaper utility to automatically scan the /tmp directory and delete any files that haven’t been accessed in a specific number of days. While excellent for saving disk space, this can be disastrous if you are running a long-term background script that writes a crucial log file to /tmp and expects it to remain there indefinitely.

Protecting Your Temp Files

To preserve your data and stop Ubuntu from automatically deleting files from the temporary directory, you must override the systemd cleanup rules.

Open your terminal (Ctrl+Alt+T). The default rules are stored in /usr/lib/tmpfiles.d/tmp.conf, but you should never edit that file directly. Instead, you create a local override. Type sudo cp /usr/lib/tmpfiles.d/tmp.conf /etc/tmpfiles.d/ and press Enter. Now, edit your new local copy by typing sudo nano /etc/tmpfiles.d/tmp.conf. Look for the line that looks like v /tmp 1777 root root 10d (the “10d” means delete after 10 days). Change the time value to a hyphen (-). The line should now read: v /tmp 1777 root root -. Press Ctrl+O to save, Enter to confirm, and Ctrl+X to exit. The systemd timer will now ignore the /tmp directory entirely.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.