Skip to content

/tmp never cleared #1278

Closed
Closed

Description

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.

Activity

  1. fpqc commented on Oct 27, 2016

    @fpqc

    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.

  2. aseering commented on Oct 27, 2016

    @aseering
    Contributor

    fpqc FYI -- /tmp is 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.

  3. S-ed commented on Nov 18, 2016

    @S-ed

    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.

  4. fpqc commented on Nov 19, 2016

    @fpqc

    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)

  5. S-ed commented on Nov 19, 2016

    @S-ed

    fpqc nope, still there, even after windows reboot.

  6. fpqc commented on Nov 19, 2016

    @fpqc

    Sed On Drvfs? On Lxfs (outside of /mnt)? Did you touch them with Windows tools?

  7. S-ed commented on Nov 19, 2016

    @S-ed

    fpqc

    C:\Users\S-ed\AppData\Local\lxss\home\s-ed.npm
    C:\Users\S-ed\AppData\Local\lxss\root.npm

    Did nothing (outside of bash).

    Try it Yourself, use:

    sudo npm install -g express
    sudo npm uninstall -g express

    edit: now I've got it ls just not showing me any files/folders started with dot ., faced same thing with .git (fixed with ls -a)

  8. bitcrazed commented on Nov 20, 2017

    @bitcrazed
    Contributor
  9. S-ed commented on Nov 21, 2017

    @S-ed

    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)

  10. therealkenc commented on Nov 21, 2017

    @therealkenc

    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.

  11. S-ed commented on Nov 21, 2017

    @S-ed

    therealkenc Yep. I thought it's caused by the same issue as the /tmp one, but it wasn't.

  12. Meinersbur commented on Nov 21, 2017

    @Meinersbur
    Author

    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.

  13. therealkenc commented on Nov 21, 2017

    @therealkenc

    Considering that "Windows Disk cleanup" is a Windows component that should be aware of special paths

    There, you used the words again. Can't say I didn't try to help you out....

  14. bitcrazed commented on Nov 21, 2017

    @bitcrazed
    Contributor

    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.

  15. Angeldude commented on Feb 14, 2018

    @Angeldude

    Has anything come of this?

  16. 8 remaining items

  17. WSLUser commented on Apr 30, 2018

    @WSLUser

    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.

  18. rigarash commented on May 22, 2018

    @rigarash

    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 ?

  19. therealkenc commented on May 22, 2018

    @therealkenc

    config setting in /etc/wsl.conf ?

    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 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.

    Something like that.

  20. therealkenc commented on May 24, 2018

    @therealkenc

    Since #344 was deemed addressed, following suit.

  21. AgDude commented on Apr 21, 2021

    @AgDude

    therealkenc #344 did not address this issue on ubuntu, the /tmp directory still grows indefinitely on a default ubuntu wsl instance.

  22. bobhy commented on Dec 12, 2021

    @bobhy

    Here is my interim workaround:
    Define a task in Windows Task Scheduler that runs the following command when my user logs in:

    c:\windows\system32\wsl.exe --user root --distribution ubuntu /etc/init-wsl

    (you know where this is going...)
    In distribution ubuntu, create a file /etc/init-wsl:

    tmpreaper 96 /tmp

    (my own version of this file also starts dbus, avahi-daemon and xrdp so I have a stable DNS name to connect to....)

  23. igoradamenko commented on Dec 22, 2021

    @igoradamenko

    Hey there.

    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?

  24. igoradamenko commented on Dec 22, 2021

    @igoradamenko

    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-tmpfiles does not clean all the files inside /tmp, so I'm not sure that it should be used 😞

  25. aradalvand commented on Jun 27, 2022

    @aradalvand

    Sorry, why was this issue closed? I'm not really sure the problem is solved here...

  26. rfc2119 commented on Jan 9, 2023

    @rfc2119

    I second this. /tmp is not cleaned on Ubuntu 22.04

  27. darkvertex commented on Jan 10, 2023

    @darkvertex

    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.)


    Knowledge credit: #6999 (comment)

  28. rgmz commented on May 22, 2024

    @rgmz

    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/
    
  29. JustTheCoolest commented on Oct 11, 2025

    @JustTheCoolest

    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

    root@DESKTOP-JG3EPBN:/tmp# stat * | grep Access
    Access: (0700/prwx------)  Uid: (    0/    root)   Gid: (    0/    root)
    Access: 2025-08-23 13:13:50.802654774 +0530
    Access: (0700/prwx------)  Uid: (    0/    root)   Gid: (    0/    root)
    Access: 2025-08-23 13:13:50.802654774 +0530
    Access: (0600/srw-------)  Uid: (    0/    root)   Gid: (    0/    root)
    Access: 2025-08-23 13:13:50.780654776 +0530
    Access: (0755/drwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
    Access: 2025-10-11 07:10:04.363065345 +0530
    Access: (0700/drwx------)  Uid: (    0/    root)   Gid: (    0/    root)
    Access: 2025-10-11 07:10:04.243065372 +0530
    Access: (0700/drwx------)  Uid: (    0/    root)   Gid: (    0/    root)
    Access: 2025-10-11 07:10:04.243065372 +0530
    Access: (0700/drwx------)  Uid: (    0/    root)   Gid: (    0/    root)
    Access: 2025-10-11 07:10:04.243065372 +0530
    Access: (0700/drwx------)  Uid: (    0/    root)   Gid: (    0/    root)
    Access: 2025-10-11 07:10:04.243065372 +0530
    Access: (0700/drwx------)  Uid: (    0/    root)   Gid: (    0/    root)
    Access: 2025-10-11 07:10:04.363065345 +0530
    Access: (0700/drwx------)  Uid: (    0/    root)   Gid: (    0/    root)
    Access: 2025-10-11 07:10:04.563065300 +0530
    Access: (0755/drwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
    Access: 2025-10-11 07:10:04.253065369 +0530
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Access: 2025-10-08 13:45:42.451875293 +0530
    Access: (0700/drwx------)  Uid: (    0/    root)   Gid: (    0/    root)
    Access: 2025-10-11 07:10:04.563065300 +0530
    Access: (0700/drwx------)  Uid: (    0/    root)   Gid: (    0/    root)
    Access: 2025-10-11 07:10:04.363065345 +0530
    Access: (0600/-rw-------)  Uid: (    0/    root)   Gid: (    0/    root)
    Access: 2025-08-09 10:35:17.434009186 +0530
    Access: (0600/-rw-------)  Uid: (    0/    root)   Gid: (    0/    root)
    Access: 2025-08-14 00:18:40.923656176 +0530
    Access: (0600/-rw-------)  Uid: (    0/    root)   Gid: (    0/    root)
    Access: 2025-10-08 13:45:46.401874564 +0530
    Access: (0600/-rw-------)  Uid: (    0/    root)   Gid: (    0/    root)
    Access: 2025-09-25 18:53:15.402746679 +0530
    Access: (0755/drwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
    Access: 2025-10-11 07:10:04.243065372 +0530
    root@DESKTOP-JG3EPBN:/tmp#
    

    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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions