How to automatically delete cache file in home directory

Just as the title says. How do I delete the cache folder in the home directory automatically?

To my understanding.
Does it make sense to delete the entire folder?
I have seen a post somewhere here recommending to make an entry in the fstab with tmpfs. I just can’t remember where. Does that make sense?

I would prefer to delete the folder on shutdown.
If this option makes sense, what is the best way to proceed?

EDIT: Found the Post here. Thank you @sammiev

1 Like

:smiley: I was just going to post the link when you edited your post.

tmpfs /home/sam/.cache tmpfs uid=1000,gid=1000,mode=750 0 0

The only thing i have to change is /home/sam/.cache to /home/swh/.cache

Just one last question. There is a folder called yay. Can I safely delete it?

I use yay myself and it gets deleted as well, never had any issues using this method as I have been using it for years. After using yay, I noticed that the yay folder is created and usually around 2 MiB to 7.7 MiB in size.

You could try experimenting with a systemd service that get executed before shutdown.

Something like:

[Unit]
Description=Clean the cache  before shutdown
DefaultDependencies=no
Before=shutdown.target 

[Service]
Type=oneshot
ExecStart=sh -c 'rm -r ~/.cache/'

[Install]
WantedBy=shutdown.target

Make a file with the above content. Call it what-you-like.service and put it in /etc/systemd/system/.

Enable it: sudo systemctl enable what-you-like.service

I haven’t tried it but it should work. In principle :wink:

Edit:

For the service to work properly, the full path to cache directory should be given (thanks to @vlkon for pointing it out.)

ExecStart=sh -c 'rm -r /home/$USER/.cache/'

Or if you want to delete just some directories:

rm -r /home/$USER/.cache/{DIR1,DIR2,DIR3}

(for example)

and name the directories you want to be removed.

1 Like

Thank you very much. I will now enter this in the fstab and if you don’t hear from me for a while, there have been problems booting up :sweat_smile:

I have no experience with self-created systemd services. I will make an entry in fstab. Thank you very much, at some point i will read up on systemd services to get an understanding of it

1 Like

This is where I found the info back then ( systemd and fstab ) and tested both ways without issue.

http://www.voleg.info/cache-tmpfs.html

1 Like

That worked exactly as intended. .cache is almost empty after booting except for the usual cache files for Plasma, which are reloaded. As soon as I start an application, such as strawberry, a new file is automatically created in the .cache folder. So the cache always stays clean.

Thousand thanks!

1 Like

I would just ask why do you want to delete cache on a shutdown? It is there to speed up some software’s start time. Some files (e.g. yay or pip packages) may require download from the internet which may not be ideal on every startup.

After emptying the cache, it is rebuilt and the ssd is not overwritten many times but rewritten. It doesn’t matter to me if something has to be downloaded again. I think it only takes milliseconds to start programs. I have this time :wink:

2 Likes

Hey @pebcak !
That does not work as intended

[Unit]
Description=Clean the cache  before shutdown
DefaultDependencies=no
Before=shutdown.target

[Service]
Type=oneshot
ExecStart=sh -c 'rm -r ~/.cache/'

[Install]
WantedBy=shutdown.target

I have marked the service as clearcache.service and started it as non-root.

➜  systemctl status --user clearcache.service
○ clearcache.service - Clean the cache  before shutdown
     Loaded: loaded (/home/swh/.config/systemd/user/clearcache.service; enabled; preset: enabled)
     Active: inactive (dead)

What could be the fault? Where did we do something wrong? Can you think of anything else?

Hmmm…

Does systemctl --user start clearcache.service reveal something more?

Run this and then the “status” command again.

No it doesnt.

This is new status

➜  systemctl status --user clearcache.service
○ clearcache.service - Clean the cache  before shutdown
     Loaded: loaded (/home/swh/.config/systemd/user/clearcache.service; enabled; preset: enabled)
     Active: inactive (dead) since Tue 2023-11-14 17:16:26 CET; 4s ago
    Process: 4158 ExecStart=sh -c rm -r ~/.cache/ (code=exited, status=0/SUCCESS)
   Main PID: 4158 (code=exited, status=0/SUCCESS)
        CPU: 1.046s

Nov 14 17:16:25 ryzen systemd[1063]: Starting Clean the cache  before shutdown...
Nov 14 17:16:26 ryzen systemd[1063]: Finished Clean the cache  before shutdown.
Nov 14 17:16:26 ryzen systemd[1063]: clearcache.service: Consumed 1.046s CPU time.

This means that the service is now activated.
But it should be activated automatically.

perhaps use full path /home/user/.cache?

Thanks, it might be so. I’ll try

Try @vlkon’s suugestion!

Also, this

seems to be suggesting that the command line in ExecStart has run successfully. I guess :sweat_smile:

Look into the cache directory and see if it is cleared out.

I am no expert but it looks like sh -c command was called successfully but did rm -r as well? :smile: :thinking:

Thanks @vlkon that was the problem. Cache fully cleared after reboot except the usual files for plasma.

1 Like

Yeah, not the first time I made the exact same mistake. :sweat_smile::rofl: