Are we supposed to manually delete the contents of /tmp?

I was under the impression that “old” files in /tmp will be regularly deleted. However, it seems to me that /tmp will just grow as long as it wants to, and nothing will be deleted. Some people say it’s better to leave /tmp alone and delete its contents only if the disk is getting full.

My question is, is /tmp really designed to not take care of itself? What are the best practices?


To answer the questions:

  • Is /tmp supposed to be emptied automatically: Yes
  • Are we supposed to delete Files in /tmp regularly and manually: No, your system will take care of them.

If you may ask yourself:

  • Can I delete files from /tmp for whatever reason (need space, want to remove traces, etc.): It depends, read on.

Filesystem Hierarchy Standard (FHS) states:

The /tmp directory must be made available for programs that require temporary files.

Programs must not assume that any files or directories in /tmp are preserved between invocations of the program.

/var/tmp/ has a similar purpose, but must not be deleted during reboot.

It is not guaranteed that /tmp/ or /var/tmp/ are cleaned up on a regular basis. This may depend on your distribution and settings, although most systems do some cleanup from time to time. See comment by mike.

If you need to to delete a file in /tmp, see first if the file is in use. You can do this easily with:

lsof /tmp/file_to_delete

If you have rights to do so, this will show the process holding the handle to that file, like process name, PID and type of the file. To show really all processes, prepend sudo or run as user root.

lsof +D /tmp

will show you all files in /tmp and directories below (+D) that are currently open. Of course you should not delete these files.

In fact when you delete a file that is still opened - if you have the rights to do so - it becomes inaccessible from the filesystem namespace, but it still exists for the processes that have an open file handle for it. After closing that handle, the file is not accessible for that process any more, and if no process has the file opened any more it is finally deleted. A process should not suppose that the file survives between subsequent open calls, but programmers are sloppy, and you never know. For that reason it's not that clever to delete files that are still in use by some programs.


I think this is OS varient dependent. I'd imagine that /tmp is typically cleared on reboot, and indeed it would not be safe for the system to clean itself up mid session as it won't know what files are active.

If you are brave you might want to throw a command into crontab which deletes files older then a certain age, but this might cause some issues if it deletes files still used. You might try a command (I have not tried it) like

find /tmp -type f -ctime +10 -exec rm {} +

Which will theoretically remove all files under /tmp older then 10 days.