[Solved] How to make a directory temporary / auto cleaning?

Hi there,
just a simple question: Is there a way to make a directory auto-cleaning itself by making it an temporary directory?
I want my Downloads-folder to auto-empty after a while.

AFAIK only /tmp empties itself on shutdown.
For other folders, you could schedule a script/command with cron.

1 Like

You could create a systemd timer or similar to run, ex every week.


Something like this might work:

~/.config/systemd/user/downloads-clean.timer

[Unit]
Description=Weekly Cleanup of User's Downloads Directory
Documentation="https://forum.manjaro.org/t/how-to-make-a-directory-temporary-auto-cleaning/37478/3"

[Timer]
OnStartupSec=5min
OnUnitActiveSec=7d

[Install]
WantedBy=timers.target

~/.config/systemd/user/downloads-clean.service

[Unit]
Description=Cleanup of User's Downloads Directory
Documentation="https://forum.manjaro.org/t/how-to-make-a-directory-temporary-auto-cleaning/37478/3"
DefaultDependencies=no
Conflicts=shutdown.target
Before=basic.target shutdown.target

[Service]
Type=oneshot
ExecStart=/usr/bin/sh -c '/usr/bin/rm -rf %h/Downloads/*'
SuccessExitStatus=DATAERR
IOSchedulingClass=idle

Then you can go ahead and run the following:

systemctl --user enable downloads-clean.timer
9 Likes

In KDE the path is ~/.config/systemd/user/ or did I somehow mess something up?

1 Like

Oops.
Both are correct, but packages should use the .local path while our own custom ones should use .config.
Good eye :slight_smile:

https://wiki.archlinux.org/index.php/Systemd/User

Similarly to system units, user units are located in the following directories (ordered by ascending precedence):

/usr/lib/systemd/user/ where units provided by installed packages belong.
~/.local/share/systemd/user/ where units of packages that have been installed in the home directory belong.
/etc/systemd/user/ where system-wide user units are placed by the system administrator.
~/.config/systemd/user/ where the user puts their own units.

(I will edit the above)

4 Likes

Thank you, that explains a lot.

Thanks a lot!

You know…
I am silly and skipped the ability for systemd to do this already.

~/.config/user-tmpfiles.d/weekly-downloads-clean.conf

R %h/Downloads/* - - - 1w -

And enable the timer systemctl enable --user systemd-tmpfiles-clean.timer

https://www.freedesktop.org/software/systemd/man/systemd-tmpfiles.html
https://www.freedesktop.org/software/systemd/man/tmpfiles.d.html

But aw well … either way I guess :wink:

5 Likes

Could we use as ExecStart something like

ExecStart=/usr/bin/sh -c '/usr/bin/find %h/Downloads -type f -ctime -7 -prune -o \(\! -mtime -7 -print0 \) | xargs -0 /bin/rm -f'

to delete only files “older” than 7 days?

I’m not sure about syntax, just had man find as reference

As an alternative to already suggested answers, why not just create a directory in /tmp and link it to wherever. Could be done in a startup script. The problem with timers is that if the timer event is triggered one second after downloading something, it will be gone.

I may not have explained well.
The last post relies on a daily timer.
The configuration file denotes age of files - files older than 1 week will be purged daily.

(which is of course different than the weekly remove everything in Downloads timer I first posted)

Wow this is an awesome feature!

It looks like you don’t have to add /* at the end to clean that folder, just tested.

…You made me double-check … and now I am not so sure the Age frame works with -R

The age field only applies to lines starting with d , D , e , v , q , Q , C , x and X . If omitted or set to " - ", no automatic clean-up is done.

It should probably be e … :yawning_face:

e

Adjust the mode and ownership of existing directories and remove their contents based on age. Lines of this type accept shell-style globs in place of normal path names. Contents of the directories are subject to time based cleanup if the age argument is specified. If the age argument is " 0 ", contents will be unconditionally deleted every time systemd-tmpfiles --clean is run.

I must be losing my mind.

Well I’ve tested on jy KDE install

D %h/.local/share/RecentDocuments - - - 1s -

and then

systemctl --user start systemd-tmpfiles-clean.service

Wow, the second solution is even better!

Please allow me a silly question though:
Eventhough I enabled systemd user units as Arch Wiki says with
systemctl --user enable systemd-tmpfiles-setup.service systemd-tmpfiles-clean.time, I cant find the directory ~/.config/user-tmpfiles.d Do I have to make it?

Yes you have to create it yourself if it does not already exist (likely as it did not for me).
And you only need to start the user timer … it will launch the service and all.

1 Like

Thanks, it worked :grinning:

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.