How to Stop Ubuntu from Automatically Deleting Temporary Files

The Missing Data

Linux systems (including Ubuntu) rely heavily on temporary files to store transient data, session tokens, and socket files. These files are typically stored in the /tmp directory. By default, modern Ubuntu distributions utilize a systemd timer (specifically systemd-tmpfiles-clean.timer) that automatically scans the /tmp directory and aggressively deletes files that haven’t been accessed in the last 10 days. While this prevents the hard drive from filling up with useless junk, it can be disastrous if you have a long-running, poorly coded scientific script or a background daemon that creates a lock-file in /tmp but doesn’t actively touch it for weeks. The system will delete the active file, causing your script to crash.

Disabling the Cleanup Timer

To protect your long-running processes and stop Ubuntu from automatically deleting temporary files in the /tmp directory, you must disable or override the systemd cleanup timer.

Open your terminal (Ctrl+Alt+T). To completely disable the automatic cleanup, you will stop and mask the systemd timer responsible for it. Type sudo systemctl stop systemd-tmpfiles-clean.timer and press Enter. Next, type sudo systemctl mask systemd-tmpfiles-clean.timer and press Enter. Masking ensures that no other service can accidentally start the timer. If you don’t want to completely disable it, but rather extend the safe period (e.g., from 10 days to 30 days), do not mask the timer. Instead, copy the default configuration file to the override directory: sudo cp /usr/lib/tmpfiles.d/tmp.conf /etc/tmpfiles.d/. Edit this new file with sudo nano /etc/tmpfiles.d/tmp.conf and change the “10d” value next to the /tmp entry to “30d”.

Get the best tech tips delivered straight to your inbox.

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