How to clean /tmp?

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?


Solution 1:

/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

Solution 2:

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)

Solution 3:

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.

Solution 4:

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.