Alt Text

A screenshot of a file manager preview window for my ~/.cache folder, which takes up 164.3 GiB and has 246,049 files and 15,126 folders. The folder was first created about 1.75 years ago with my system

  • Skull giver@popplesburger.hilciferous.nl
    link
    fedilink
    arrow-up
    42
    arrow-down
    1
    ·
    7 months ago

    You could try something like:

    $HOME/.config/systemd/user/clean-user-cache.service
    [Unit]
    Description=Clear cache
    After=network.target
    
    [Service]
    Type=oneshot
    ExecStart=/usr/bin/rm -r %h/.cache/* %h/.cache/.*
    
    [Install]
    WantedBy=default.target
    
    $HOME/.config/systemd/user/clean-user-cache.timer
    [Unit]
    Description=Clear user's cache every month
    
    [Timer]
    # Runs on midnight on the first day of the month
    OnCalendar=monthly
    # Wait a random time between 0 and 10 minutes before invocation. This prevents your FS getting hammered if you deploy this script for multiple users that all log on after a reboot.
    RandomizedDelaySec=10min
    # Enable persistence to run the command even if the machine wasn't on at the time the timer was supposed to trigger
    Persistent=true
    
    To enable the monthly timer:

    $ systemctl --user daemon-reload $ systemctl --user enable clean-user-cache.timer

    To clean the cache once, right now:

    $ systemctl --user start clean-user-cache.service

    To see the output of the service (and past executions, if they happened recently):

    $ journalctl --user -u clean-user-cache.service

    This relies on a systemd user service (better not do rms as root, just in case) so you need to log in for it to work. You could also use a system service with User= and Group= in case you’re removing the cache on a server you rarely ever log into.

    • BaroqueInMind@kbin.social
      link
      fedilink
      arrow-up
      9
      ·
      7 months ago

      This is the good shit I miss from reddit. Thank you for posting a systemd service config, I’m going to implement this.

    • Zangoose@lemmy.worldOP
      link
      fedilink
      English
      arrow-up
      4
      ·
      7 months ago

      Thanks for this! I’ve been meaning to start getting into learning more about systemd and making services, this is super detailed and gives me a pretty good starting point!