How is the /tmp
directory cleaned up? Is it automatic? If so, how frequently is it cleaned up?

- 14,223

- 58,691
8 Answers
Note! This answer is outdated since at least ubuntu 14.04. See other answers for current situation and if they prove correct then upvote them furiously. Also post comment so I can put link here to current correct answer.
For 14.04 see https://askubuntu.com/a/759048/1366
For 16.10 see https://askubuntu.com/a/857154/453746
Old answer from 2011:
The cleaning of /tmp
is done by the upstart script /etc/init/mounted-tmp.conf
. The script is run by upstart everytime /tmp
is mounted. Practically that means at every boot.
The script does roughly the following: if a file in /tmp
is older than $TMPTIME
days it will be deleted.
The default value of $TMPTIME
is 0, which means every file and directory in /tmp
gets deleted. $TMPTIME
is an environment variable defined in /etc/default/rcS
.

- 18,386
-
1That's not true for 14.04 anymore (the script does not exist anymore). – Martin Schröder Apr 19 '16 at 12:02
-
@Martin Schröder - the script exists on my system & it is a clean install of 14.04 and tmpreaper doesn't exist! – Lance Holland Jun 17 '16 at 13:18
-
I am using arch linux now so I cannot verify. Sorry. Please someone verify this and comment or edit my answer. – Lesmana Jun 18 '16 at 12:31
-
4in ubuntu 16.04 tmpreaper is abandoned as un-secure please see: https://fossies.org/linux/tmpreaper/debian/README.security – tamerlaha Oct 27 '16 at 15:14
-
2No. In Ubuntu 16.04 it is simply superseded by a systemd mechanism. Read that document, and what you will actually find is an explanation of why the security analysis claiming insecurity is flawed. – JdeBP Sep 16 '18 at 09:40
The directory is cleared by default at every boot, because TMPTIME
is 0 by default.
Here you can change the time in the following file:
/etc/default/rcS
TMPTIME
says how frequent the tmp dir sould be cleared in days

- 1,693
-
13Clearing at every boot is not ideal for a machine that is never rebooted, like a server. I've got over 500,000 files taking 5Gb space in my /tmp because my server has 378 days uptime. I'm reluctant to reboot it, simply because clearing those files on reboot make take hours. – rjmunro Jan 25 '12 at 11:47
-
12
-
1
-
-
I'm with @KenSharp in this one: a CRON job does the trick for a server with such a big uptime. See http://askubuntu.com/questions/2368/how-do-i-set-up-a-cron-job – Yuri Sucupira Apr 16 '16 at 00:08
-
1It looks like Ubuntu/systemd already provides a solution. I'd say more but.... systemd. – Ken Sharp May 13 '17 at 07:50
-
3
-
@rjmunro on the other you can't just wipe out the directory while it's up. Can we determine what process (PID and command) created the file and is it still alive? That would help sorting this out. – timuçin Feb 18 '21 at 00:37
-
1@timuçin My comment was from 8 years ago, but yes, you can't just trash /tmp while things are up - something could break. Mainly I was just making the point that deleting /tmp on reboot is not necessarily a good strategy. A cron job that deletes files older than a certain age is usually better. – rjmunro Feb 22 '21 at 11:01
-
@rjmunro I'm not arguing your point. Just speculating on how it could be implemented. – timuçin Mar 04 '21 at 16:47
I'm checking this on Ubuntu 16.10. I can certify that editing /etc/default/rcS has no effect at all anymore and the files in tmp are wiped out by reboot no matter what you put in that file. As others mention, tmpreaper is no longer used.
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.
The advantage of the new setup is that a file cleaner can still run even if the system is not rebooted (as in the case of an always on server). That's a big plus, I think.

- 3,743
-
11
-
1I found that you can preserve the original file permissions and owner by using hyphens:
d /tmp/ - - - 20d
– Dave Yarwood May 22 '18 at 15:38 -
5Also worth noting: you can test your configuration by running the cleaning job manually:
systemctl start systemd-tmpfiles-clean
– Dave Yarwood May 22 '18 at 15:38 -
-
3
-
5Creating
/etc/tmpfiles.d/tmp.conf
overrides/usr/lib/tmpfiles.d/tmp.conf
causing the configuration for the other directories there to be lost if not copied (e.g.,/var/tmp/systemd-private-%b-*
). See the source. – Mihai Capotă Aug 23 '19 at 19:14 -
New link to ubuntus config file: https://git.launchpad.net/ubuntu/+source/systemd/tree/tmpfiles.d/tmp.conf?h=ubuntu%2Ffocal It contains
q /tmp 1777 root root 10d
andq /var/tmp 1777 root root 30d
– mgutt Sep 09 '20 at 22:33
While the /tmp
folder is not a place to store files long-term, occasionally you want to keep things a little longer than the next time you reboot, which is the default on Ubuntu systems. I know a time or two I’ve downloaded something to /tmp
during testing, rebooted after making changes and then lost the original data again. This can be changed if you’d like to keep your /tmp
files a little bit longer.
Changing the /tmp
Cleanup Frequency
The default setting that tells your system to clear /tmp
at reboot is held in the /etc/default/rcS
file. The value we’ll look at is TMPTIME
.
The current value of TMPTIME=0
says delete files at reboot despite the age of the file. Changing this value to a different (positive) number will change the number of days a file can survive in /tmp
.
TMPTIME=7
This setting would allow files to stay in /tmp
until they are a week old, and then delete them on the next reboot. A negative number (TMPTIME=-1
) tells the system to never delete anything in /tmp
. This is probably not something you want, but is available.

- 6,900

- 42,002
-
1good explanation. But in which script is the cleanup command? I have seen
/etc/init/mounted-temp.conf
, but it has the linestart on mounted MOUNTPOINT=/tmp
that make me think it is non applicable. – enzotib Jan 09 '11 at 19:56 -
8If you don't want a file to be removed automatically, put it in
/var/tmp
instead of/tmp
. – Gilles 'SO- stop being evil' Jan 10 '11 at 21:29 -
1It's also handy to keep files you don't want to lose (rendered video frames) when your machine crashes, possibly due to OOM. A better solution would be to fix the problem, of course. :-) – Ken Sharp Mar 08 '14 at 14:28
-
-
I think it would be better to keep such longer-term temp files in
/var/tmp
(as Gilles said). Then you can mount/tmp
as tmpfs (as Arch Linux does). – Guildenstern Feb 26 '21 at 10:19
In Ubuntu 14.04 this is done by tmpreaper
, which is called daily by cron (from /etc/cron.daily
). The program can be configured via /etc/default/rcS
and /etc/tmpreaper.conf
.

- 6,900
-
on my system, tmpreaper wasn't in /etc/cron.daily -- but I was able to install it with apt-get – Joe Germuska Nov 03 '17 at 19:46
-
2Note: you should NOT install tmpreaper on 16 and 18, b/c it has a built-in system – jitbit Mar 22 '20 at 09:34
In a systemd
Ubuntu (15.10 and newer), this is done by systemd, using systemd-tmpfiles-setup.service
, systemd-tmpfiles-clean.service
, and systemd-tmpfiles-clean.timer
:
# /lib/systemd/system/systemd-tmpfiles-setup.service
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
[Unit]
Description=Create Volatile Files and Directories
Documentation=man:tmpfiles.d(5) man:systemd-tmpfiles(8)
DefaultDependencies=no
After=local-fs.target systemd-sysusers.service systemd-journald.service
Before=sysinit.target
Conflicts=shutdown.target initrd-switch-root.target
Before=shutdown.target initrd-switch-root.target
RefuseManualStop=yes
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=systemd-tmpfiles --create --remove --boot --exclude-prefix=/dev
SuccessExitStatus=DATAERR CANTCREAT
LoadCredential=tmpfiles.extra
LoadCredential=login.motd
LoadCredential=login.issue
LoadCredential=network.hosts
LoadCredential=ssh.authorized_keys.root
# /lib/systemd/system/systemd-tmpfiles-clean.service
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
[Unit]
Description=Cleanup of Temporary Directories
Documentation=man:tmpfiles.d(5) man:systemd-tmpfiles(8)
DefaultDependencies=no
Conflicts=shutdown.target
After=local-fs.target time-sync.target
Before=shutdown.target
[Service]
Type=oneshot
ExecStart=/bin/systemd-tmpfiles --clean
IOSchedulingClass=idle
# /lib/systemd/system/systemd-tmpfiles-clean.timer
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
[Unit]
Description=Daily Cleanup of Temporary Directories
Documentation=man:tmpfiles.d(5) man:systemd-tmpfiles(8)
[Timer]
OnBootSec=15min
OnUnitActiveSec=1d
You can change the timer behaviour itself using systemctl edit systemd-tmpfiles-clean.timer
, and using various systemd Timer
configuration options (see man 5 systemd.timer
).
So systemd-tmpfiles-clean
runs once per day, and systemd-tmpfiles-setup
runs once per boot. The files it cleans can be extended using /etc/tmpfiles.d
(mentioned in another answer).
It's interesting to note that as default /etc/tmpfiles.d
is empty. The file where the /tmp
policy is defined is here:
/usr/lib/tmpfiles.d/tmp.conf
Actual content:
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
See tmpfiles.d(5) for details
Clear tmp directories separately, to make them easier to override
D /tmp 1777 root root -
#q /var/tmp 1777 root root 30d
The 30d line is disabled but would be run every 30 days by systemd-tmpfiles-clean.service
. The D
line will cause systemd-tmpfiles-setup.service
to create the directory and remove its contents at every boot. So, as already noted, to override it, you can see the other answer about tmpfiles.d.
-
I've changed the timer to 20min, and i see that status command shows that it is actually using 20min configuration. but my problem is that
/tmp
still not cleaned up. and i need it to be cleaned up. even manual startsudo systemctl start systemd-tmpfiles-clean
doesn't do the trick. Any ideas why? – user2932688 Jun 27 '19 at 13:34 -
@ValerioBozz see the answer I linked to: https://askubuntu.com/a/857154/158442 – muru Dec 28 '21 at 11:33
-
-
-
@muru Where is the documentation that "So systemd-tmpfiles-clean runs on shutdown, and once per day otherwise"? Looking at the service and logs, it doesn't run on shutdown. Furthermore, the
D /tmp 1777 root root -
is handled bysystemd-tmpfiles-setup
notsystemd-tmpfiles-clean
because it does not have an age – Daniel T Feb 29 '24 at 14:22 -
@DanielT these lines:
DefaultDependencies=no ... Before=shutdown.target
(compare with https://unix.stackexchange.com/a/479048/70524 or https://unix.stackexchange.com/a/284773). Yes, the default configuration does not have an age, that's why I have referred to the other earlier answer which does show how to set an age. – muru Feb 29 '24 at 14:58 -
@muru What does
DefaultDependencies=no ... Before=shutdown.target
have to do with running a service on shutdown?Before=
creates an ordering dependency not a requirement dependency. That means that line only delaysshutdown.target
until after this service, and most units haveBefore=shutdown.target
if you runsystemctl show <name>.service
– Daniel T Feb 29 '24 at 15:06 -
If you look at a comment in your link, https://unix.stackexchange.com/questions/39226/how-to-run-a-script-with-systemd-right-before-shutdown/479048#comment542729_39604 , it's the
WantedBy=
line that actually creates the requirement dependency. That line is not present in/lib/systemd/system/systemd-tmpfiles-clean.service
. There is noExecStop=
line either. – Daniel T Feb 29 '24 at 15:22 -
@DanielT hmm, you're right. It should be once on startup and once a day. I'll take a look over the weekend to see if I can find what made me think it ran on shutdown (it's been 6 years since I wrote this). You can go ahead and edit it if you want, if not, I'll fix it sometime next week) – muru Feb 29 '24 at 15:33
Before 14.04:
It is cleaned up every time you reboot.

- 281
-
Appears with 14.04+ it only uses tmpreaper, not the "per boot" script FWIW – rogerdpack Sep 08 '16 at 16:03
-
My 14.04 systems all clean up on reboot. Never even heard of
tmpreaper
. – Ken Sharp May 13 '17 at 07:51 -
On one of our servers running Ubuntu, we have a script to remove files in /tmp and it runs nightly.
The script is:
#!/bin/sh
# Clean file and dirs more than 3 days old in /tmp nightly
/usr/bin/find /tmp -type f -atime +2 -mtime +2 |xargs /bin/rm -f &&
/usr/bin/find /tmp -type d -mtime +2 -exec /bin/rm -rf '{}' \; &&
/usr/bin/find /tmp -type l -ctime +2 |xargs /bin/rm -f &&
/usr/bin/find -L /tmp -mtime +2 -print -exec rm -f {} \;
Just save the contents above to a file chmod 775 the file and create a cron entry to run it. Since this is a web server we don't want to reboot it for obvious reasons.
-
7
-
13The last line is extremely dangerous. Normally, everyone is able to run
ln -s /usr /tmp/kaboom
or evenln -s /* /tmp/
... – Daniel Alder Feb 28 '16 at 22:25
tmpfs /tmp tmpfs defaults,noatime,mode=1777 0 0
in /etc/fstab. – Anonymous Nov 16 '11 at 23:43