144
rizhas@rizhas-laptop:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda7        67G   58G  5,2G  92% /
none            4,0K     0  4,0K   0% /sys/fs/cgroup
udev            1,5G   12K  1,5G   1% /dev
tmpfs           303M  1,2M  302M   1% /run
none            5,0M     0  5,0M   0% /run/lock
none            1,5G  348K  1,5G   1% /run/shm
none            100M   80K  100M   1% /run/user
overflow        1,0M  1,0M     0 100% /tmp
overflow        1,0M  1,0M     0 100% /tmp

How to clean up /tmp?

kiri
  • 28,246
  • 16
  • 81
  • 118
Rizhas
  • 1,549
  • 2
  • 10
  • 4

9 Answers9

220

/tmp is supposed to be cleaned up on reboot, but if you don't reboot (which is normal for servers), clean up will not happen

find /tmp -ctime +10 -exec rm -rf {} +

will delete all files and folders older than 10 days. you may want to add it to the daily cron.


UPDATE

In comments below @sfussenegger recommends a slightly different format of this command that may be better suited to your needs and to the system you're operating on.

sudo find /tmp -type f -atime +10 -delete

Here the command is using sudo to make sure everything is deleted (or you could run it as root), operating on files that haven't been accessed for more than 10 days and only deletes files, not folders. It also uses -delete to avoid having to execute rm command

Andrei R
  • 2,301
  • 3
    instead of creation time (-ctime) you may want to use access time (-atime) instead - of course only if the filesystem keeps this information (check for noatime in /etc/fstab) – sfussenegger Aug 02 '17 at 07:30
  • 10
    ok, please bear with me but there's one more thing: you want to search files only (-type f). otherwise you may find that the parent of a directory tree hasn't been accessed in a while and delete all its content that could potentially still be in use. so the final command I'd recommend is sudo find /tmp -type f -atime +10 -delete – sfussenegger Aug 02 '17 at 07:41
  • 2
    @sfussenegger, perhaps your could promote your comments to an answer? I think it is better than being "hidden" in the comments. – Jonathan Aug 17 '17 at 12:14
  • sudo find /tmp -type f,s -atime +10 -delete could be useful to also delete socket files. – Christian Toffolo Aug 12 '21 at 11:13
45

You can assume that anything inside a tmp directory (/tmp/ /usr/tmp etc) can be deleted. BEFORE you start deleting stop all programs and services you are using since /tmp/ can be used by programs to temporarily store information for that session. So do a sudo service mysql stop and sudo service apache2 stop if you have a mysql and/or apache running. The name of the files in the /tmp/ directory most times give a clue to what program they belong.

So from command line...

cd /tmp/
pwd
sudo rm -r *

will empty the /tmp/ directory and remove all files and subdirectories. Be careful to type it correctly. The command pwd in there is not necessary but should show /tmp.

If you want it interactively (so you need to confirm deleting):

cd /tmp/
sudo rm -ri *

Also worth noting that a reboot will clear /tmp aswell as shown here: How is the /tmp directory cleaned up? So if /tmp/ is full of files after a reboot you need to investigate where those files originate from.

I also would like to state that 1 Mb for /tmp is not a lot of space. Are you using MySQL? See https://unix.stackexchange.com/a/76058/10017 on how to fix this (thanks @drc)

dessert
  • 39,982
Rinzwind
  • 299,756
  • 2
    the "without any harm" is true for files dating from before the last reboot. Otherwise it could be in use by a current program and (for that program) may cause issues... for example I have scripts that create files in /tmp, and then re-reads them to do the next step(s) of processing. deleting/truncating them in between will disrupt the program's actions (and could even lead to dangerous outcomes, depending what those files are used for) – Olivier Dulac Nov 21 '13 at 13:11
  • ... so using a who –b (date of last boot), and then creating a file /boottime dated at the time of booting ( touch ....... /boottime), will allow one to do a safe find /tmp -type f \( ! -newer /boottime \) -delete \;, deleting only files older than the last boot – Olivier Dulac Nov 21 '13 at 13:19
  • May I use these comments to expand the answer? Mind you: the /tmp here is probably from mysql. – Rinzwind Nov 21 '13 at 13:27
  • 4
    As a person using mktemp regularly in my scripts, I strongly disagree with the "without any harm" statement. – hytromo Nov 21 '13 at 13:29
  • @Rinzwind: please do :) I mentionned this on your answer to complete it (and to correct the statement about "harm"). – Olivier Dulac Nov 21 '13 at 13:53
  • 4
    If /tmp contains anything older than the last reboot, then the startup scripts haven't done their jobs. – Simon Richter Oct 01 '14 at 13:55
  • Oh lord. Maybe it's me, but I do not want to have any sudo rm -r * in my .bash_history. Or my clipboard. Or anywhere, to be honest. Maybe sudo rm -r /tmp/*? – bers Feb 22 '21 at 18:55
  • Just imagine anyone copy-and-pasting the above two-/three-liners into a terminal that has anything typed in it. It basically runs foocd /tmp and then continues to clean the current working directory. Shudder. – bers Feb 22 '21 at 18:59
19

The tmpreaper program can be used to clean up /tmp periodically. This program deletes everything that has not been accessed in a given timeframe, typically two weeks. For this to work properly, the filesystem it is on should have the atimes option enabled. If you use a tmpfs, which it appears you are doing, then you should be fine.

Of course, rebooting also clears /tmp, but that would be boring.

  • It is surprising to me that this is not a standard option in the Ubuntu and Debian. It is fairly common for me to run into relatively unprivileged user processes filling up /tmp. – sage Mar 23 '17 at 00:28
  • 1
    Yes, but it's a policy that needs to be set by a system administrator. If you have long-running services that expect their data to be kept, any default but "no cleaning" would be dangerous. – Simon Richter Mar 23 '17 at 11:00
6

The directory /tmp means temporary.

This directory stores temporary data. You don't need to delete anything from it, the data contained in it gets deleted automatically after every reboot.

Still if you want to delete the data present in it use

sudo rm -r /tmp/*

deleting from it won't cause any problem as these are temporary files.

Tarun
  • 4,245
  • 13
  • 50
  • 74
6

Be careful before running a command like rm -r ./*. Once you run it, it will be very difficult or impossible to recover any data.

All will be removed. Make sure that the directory you are deleting in is right.

There is a safer way to handle things.

# sudo rm -r /tmp/*

That way, when you accidentally run this command from inside your shell history, it won't delete the wrong files (unless you're keeping them in /tmp).

3

The /tmp directory was cleared by default at every boot, because TMPTIME is 0 by default.

Avinash Raj
  • 78,556
1

If your “/tmp” mount on a linux filesystem is mounted as overflow (often sized at 1MB), this is likely due to you not specifying “/tmp” as its own partition and your root filesystem filled up and “/tmp” was remounted as a fallback. To fix this after you’ve cleared space, just unmount the fallback and it should remount at its original point:

sudo umount overflow

if device is busy use

sudo umount -l overflow
ck reddy
  • 111
  • 1
0

Here's what worked for me:

sudo reboot

After a runaway process filled up my drive through the /tmp directory, I too considered rm -rf /tmp/* but instead just rebooted and df of my root directory went from 99% full to 77% full.

Ye ol' adage strikes again: "Did you turn it off and then on again?"

0

use built-in tmpfiles.d instead of. Check this answer: https://askubuntu.com/a/857154/453746

I think the right answer is that Ubuntu 16.10 has a new setup. There is a folder /etc/tmpfiles.d, documented in the man page "tmpfiles.d". In that folder, one should place a configuration file to control whether the /tmp is to be erased. This is what I am doing to stop reboots from erasing files in /tmp unless they are 20 days old:

#/etc/tmpfiles.d/tmp.conf

d /tmp 1777 root root 20d Replace "20d" by "-" if you never want files deleted. This is my best effort, that man page is nearly impenetrable with detail.