You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Ubuntu deletes all files in /tmp when booting. Other distributions (eg. RedHat) have a cronjob tmpwatch that deletes these files or even use tmpfs to mount /tmp.
WSL does not 'boot' nor has anacron running. The Windows Disk cleanup program does not consider the files in %USERPROFILE%/AppData/Local/lxss/rootfs/tmp. Hence these are never deleted (automatically).
My suggestion is to clean /tmp when the last lxss process closes. It already terminates background processes when there is no more bash session. At that point no process can access files in /tmp anymore and by Filesystem Hierarchy Standard files cannot be expected to be kept.
Reacted by Doodad/Doodead, rodrymbo, Boshi Lian, Alexander Kim, Andrew Harper, Eran Tiktin, Pierre Beaucamp, Klas Mellbourn, Avatar Ng, Wanlin Wang 王万霖 and 15 more
Michael Kruse (@Meinersbur) I wonder if they're going to make it actual in-RAM storage like it is on Linux. If that's the plan, they might not do it this way.
It's not only /tmp all the files that You deleting under bash are left on the hdd.
I'm using npm, and it's updating files quite often, my npm folder is clean if you look from bash. But all the files are on hdd if I look in lxss folder.
Rich Turner (@bitcrazed) Yeah. I've kinda learned this lesson last year.
Issue was related to bash was calling windows version of the npm, instead of linux one.
(IIRC now it's fixed)
The ask in the original post here was that WSL /tmp is not being scrubbed, but should be, like Real Ubuntu. The OP then went and made the terribly unfortunate mistake of using the words "Windows Disk cleanup" and mentioning win32 paths. All hope was pretty much lost at that point.
On recent versions of Ububtu /tmp is cleaned by tmpreaper, which of course does not run on WSL because no cron. More here.
This is a dupe of #511, or, alternately, a feature request (with the 'init' label) if one subscribes to the idea that WSL init should do some systemd things like #2530.
therealkenc I think there is more than one way to implement this, Running tmpreaper would be one possibility, but not the only one. Considering that "Windows Disk cleanup" is a Windows component that should be aware of special paths, I don't think its terribly unfortunate. One could install a Disk Cleanup Handler that runs eg. bash -c "rm -rf /tmp/*". However, I would not consider it the best option.
Yeah, no. We won't be relying on WinDiskCleanup to non-deterministically clean tmpfs in WSL - that's something we do when terminating a session.
Just got off a team-sync call where we briefly discussed some of the init challenges and what we might be able to do. Will be meeting post-turkey-coma to discuss in more detail.
It's part of the userspace that's pulled down with the image. Just open WSL, cd / and ls. You'll see a /tmp just as you observed from discovering the Windows path. Just rm -rf it for now or create a script to do that for you.
I agree that this is the distro policy whether cleaning /tmp on boot or not.
But in order to change this feature from WSL side, how about add config setting in /etc/wsl.conf ?
It should be made as general as possible though, to address Rich's tough questions. It should probably be able to run arbitrary scripts, be it tmpreaper or tmpwatch (which are the same thing forked over a spat nearly 20 years ago), or any other script for that matter. Maybe there could be some kind of flexible syntax in /etc/wsl.conf for scheduling when the scripts are run. Users should probably be able to set the year, the month of the year, day of the week, hour, minute. Perhaps there could be some kind of special character, asterisk maybe, to indicating running the script every week/day/hour/etc instead of a specified one. There could also be a different character, question mark maybe, to indicate running it only at startup.
I've run into the same problem — the /tmp directory is not cleared. I'm using Ubuntu 20.04. If I'm right, the distro should automatically clear /tmp on boot due to the policies described in /usr/lib/tmpfiles.d/tmp.conf (see man tmpfiles.d), which are probably copied from systemd's sources due to its installation process.
But suddenly my /usr/lib/tmpfiles.d/tmp.conf looks like this:
# 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
- in the first rule means “never”, the second line is even better — it's commented. So, this file actually defines the current behavior, and this is probably the reason why my /tmp is never cleared.
I'm sure that I didn't touch this file by myself. However it looks very strange.
So, I'm here to ask those of you who encounter the same problem on Ubuntu 18.04 or newer. Would you mind to check your /usr/lib/tmpfiles.d/tmp.conf? Does it look like the one from systemd's repo or like mine?
It looks like systemd-tmpfiles (which should analyze the config I posted above) is not fired up during the boot. Probably because it should be started by systemd, but according to #994 systemd does not work :-(
As an alternative, I guess, it should be possible to run this command on cron, or use tmpreaper.
But for some reason systemd-tmpfiles does not clean all the files inside /tmp, so I'm not sure that it should be used 😞
FYI if you enable the new systemd support in WSL2, afterwards you can enable tmp.mount to get a proper tmpfs /tmp mount when WSL initializes, like so: sudo systemctl enable /usr/share/systemd/tmp.mount
After the above and restarting WSL, you can check if it worked via mount | grep /tmp and you should see a line like tmpfs on /tmp type tmpfs (rw,nosuid,nodev).
You can obviously confirm further by "touching" a file (ie touch /tmp/potato) then exit WSL (wsl --shutdown) and then go back in a wsl shell and the file should be gone (ie ls /tmp/potato should fail.)
Reacted by yhkee0404, Sam Stephens, Jurrian Fahner, Glen, kanjieater, SymphonySimper, Christian Boyle, Maximiliano Gonzalez, hugovanversendaal, soredake and 6 more
FYI if you enable the new systemd support in WSL2, afterwards you can enable tmp.mount to get a proper tmpfs /tmp mount when WSL initializes, like so: sudo systemctl enable /usr/share/systemd/tmp.mount
One potential caveat: doing this replaces your /tmp mount, it doesn't clear data in your existing (old) mount.
In my case, I had to disable /usr/share/systemd/tmp.mount, purge the old data, then re-enable it.
# old
$ du -sh /mnt/wslg/distro/tmp/
515G /mnt/wslg/distro/tmp/
# new
$ du -sh /tmp/ 2>/dev/null
185M /tmp/
Reacted by rfc2119, Alan Fregtman and Lukas Dullinger
I'm not sure if this issue was resolved in the official WSL versions of the distros, but in my Ubuntu 22.04 server edition that was manually installed into WSL from the official image on Ubuntu's website, this issue is still present
Being able to write cronjobs is not considered a fix. It should be done without ever needing a user to set it up, as this is default expected behaviour
Activity
fpqc commentedon Oct 27, 2016
Michael Kruse (@Meinersbur) I wonder if they're going to make it actual in-RAM storage like it is on Linux. If that's the plan, they might not do it this way.
aseering commentedon Oct 27, 2016
fpqc FYI --
/tmpis not in-RAM on my Ubuntu 16.04 VM. It's not mounted at all; it's just a directory under/, which is a regular ext4 filesystem.S-ed commentedon Nov 18, 2016
It's not only /tmp all the files that You deleting under bash are left on the hdd.
I'm using npm, and it's updating files quite often, my npm folder is clean if you look from bash. But all the files are on hdd if I look in lxss folder.
fpqc commentedon Nov 19, 2016
Alexey Mak (@S-ed) I believe it does clean them up after you end the Linux Instance (right now this means close all instances of bash.exe)
S-ed commentedon Nov 19, 2016
fpqc nope, still there, even after windows reboot.
fpqc commentedon Nov 19, 2016
Sed On Drvfs? On Lxfs (outside of /mnt)? Did you touch them with Windows tools?
S-ed commentedon Nov 19, 2016
fpqc
Did nothing (outside of bash).
Try it Yourself, use:
edit: now I've got it
lsjust not showing me any files/folders started with dot., faced same thing with.git(fixed withls -a)bitcrazed commentedon Nov 20, 2017
Alexey Mak (@S-ed) please READ THIS - we STRONGLY recommend you do NOT spelunk into the Linux filesystem using Windows apps and tools. https://blogs.msdn.microsoft.com/commandline/2016/11/17/do-not-change-linux-files-using-windows-apps-and-tools/
S-ed commentedon Nov 21, 2017
Rich Turner (@bitcrazed) Yeah. I've kinda learned this lesson last year.
Issue was related to bash was calling windows version of the npm, instead of linux one.
(IIRC now it's fixed)
therealkenc commentedon Nov 21, 2017
The ask in the original post here was that WSL
/tmpis not being scrubbed, but should be, like Real Ubuntu. The OP then went and made the terribly unfortunate mistake of using the words "Windows Disk cleanup" and mentioning win32 paths. All hope was pretty much lost at that point.On recent versions of Ububtu
/tmpis cleaned bytmpreaper, which of course does not run on WSL because nocron. More here.This is a dupe of #511, or, alternately, a feature request (with the 'init' label) if one subscribes to the idea that WSL
initshould do somesystemdthings like #2530.S-ed commentedon Nov 21, 2017
therealkenc Yep. I thought it's caused by the same issue as the
/tmpone, but it wasn't.Meinersbur commentedon Nov 21, 2017
therealkenc I think there is more than one way to implement this, Running tmpreaper would be one possibility, but not the only one. Considering that "Windows Disk cleanup" is a Windows component that should be aware of special paths, I don't think its terribly unfortunate. One could install a Disk Cleanup Handler that runs eg.
bash -c "rm -rf /tmp/*". However, I would not consider it the best option.therealkenc commentedon Nov 21, 2017
There, you used the words again. Can't say I didn't try to help you out....
bitcrazed commentedon Nov 21, 2017
Yeah, no. We won't be relying on WinDiskCleanup to non-deterministically clean tmpfs in WSL - that's something we do when terminating a session.
Just got off a team-sync call where we briefly discussed some of the init challenges and what we might be able to do. Will be meeting post-turkey-coma to discuss in more detail.
Angeldude commentedon Feb 14, 2018
Has anything come of this?
8 remaining items
WSLUser commentedon Apr 30, 2018
It's part of the userspace that's pulled down with the image. Just open WSL,
cd /andls. You'll see a/tmpjust as you observed from discovering the Windows path. Justrm -rfit for now or create a script to do that for you.rigarash commentedon May 22, 2018
I agree that this is the distro policy whether cleaning /tmp on boot or not.
But in order to change this feature from WSL side, how about add config setting in /etc/wsl.conf ?
therealkenc commentedon May 22, 2018
Absolutely.
It should be made as general as possible though, to address Rich's tough questions. It should probably be able to run arbitrary scripts, be it
tmpreaperortmpwatch(which are the same thing forked over a spat nearly 20 years ago), or any other script for that matter. Maybe there could be some kind of flexible syntax in/etc/wsl.conffor scheduling when the scripts are run. Users should probably be able to set the year, the month of the year, day of the week, hour, minute. Perhaps there could be some kind of special character, asterisk maybe, to indicating running the script every week/day/hour/etc instead of a specified one. There could also be a different character, question mark maybe, to indicate running it only at startup.Something like that.
therealkenc commentedon May 24, 2018
Since #344 was deemed addressed, following suit.
AgDude commentedon Apr 21, 2021
therealkenc #344 did not address this issue on ubuntu, the /tmp directory still grows indefinitely on a default ubuntu wsl instance.
bobhy commentedon Dec 12, 2021
Here is my interim workaround:
Define a task in Windows Task Scheduler that runs the following command when my user logs in:
(you know where this is going...)
In distribution
ubuntu, create a file/etc/init-wsl:(my own version of this file also starts dbus, avahi-daemon and xrdp so I have a stable DNS name to connect to....)
igoradamenko commentedon Dec 22, 2021
Hey there.
I've run into the same problem — the
/tmpdirectory is not cleared. I'm using Ubuntu 20.04. If I'm right, the distro should automatically clear/tmpon boot due to the policies described in/usr/lib/tmpfiles.d/tmp.conf(seeman tmpfiles.d), which are probably copied from systemd's sources due to its installation process.But suddenly my
/usr/lib/tmpfiles.d/tmp.conflooks like this:-in the first rule means “never”, the second line is even better — it's commented. So, this file actually defines the current behavior, and this is probably the reason why my/tmpis never cleared.I'm sure that I didn't touch this file by myself. However it looks very strange.
So, I'm here to ask those of you who encounter the same problem on Ubuntu 18.04 or newer. Would you mind to check your
/usr/lib/tmpfiles.d/tmp.conf? Does it look like the one from systemd's repo or like mine?igoradamenko commentedon Dec 22, 2021
Ah, nevermind.
It looks like
systemd-tmpfiles(which should analyze the config I posted above) is not fired up during the boot. Probably because it should be started by systemd, but according to #994 systemd does not work :-(As an alternative, I guess, it should be possible to run this command on cron, or use
tmpreaper.But for some reason
systemd-tmpfilesdoes not clean all the files inside/tmp, so I'm not sure that it should be used 😞aradalvand commentedon Jun 27, 2022
Sorry, why was this issue closed? I'm not really sure the problem is solved here...
rfc2119 commentedon Jan 9, 2023
I second this. /tmp is not cleaned on Ubuntu 22.04
darkvertex commentedon Jan 10, 2023
FYI if you enable the new systemd support in WSL2, afterwards you can enable
tmp.mountto get a proper tmpfs /tmp mount when WSL initializes, like so:sudo systemctl enable /usr/share/systemd/tmp.mountAfter the above and restarting WSL, you can check if it worked via
mount | grep /tmpand you should see a line liketmpfs on /tmp type tmpfs (rw,nosuid,nodev).You can obviously confirm further by "touching" a file (ie
touch /tmp/potato) then exit WSL (wsl --shutdown) and then go back in a wsl shell and the file should be gone (iels /tmp/potatoshould fail.)Knowledge credit: #6999 (comment)
rgmz commentedon May 22, 2024
One potential caveat: doing this replaces your
/tmpmount, it doesn't clear data in your existing (old) mount.In my case, I had to disable
/usr/share/systemd/tmp.mount, purge the old data, then re-enable it.JustTheCoolest commentedon Oct 11, 2025
I'm not sure if this issue was resolved in the official WSL versions of the distros, but in my Ubuntu 22.04 server edition that was manually installed into WSL from the official image on Ubuntu's website, this issue is still present
Being able to write cronjobs is not considered a fix. It should be done without ever needing a user to set it up, as this is default expected behaviour