brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: systemctl clean wipes the cache or state of a systemd service

Systemd
~/tips/tip-systemd-clean

sudo systemctl clean --what=cache myapp.service

Delete a service's cache, state or log folders with one command, without guessing paths.

Services that use StateDirectory=, CacheDirectory= and friends have their folders managed by systemd. systemd can also empty them for you.

Stop the service first, then pick what to remove:

sudo systemctl stop myapp.service
sudo systemctl clean --what=cache myapp.service
sudo systemctl start myapp.service

The man page lists runtime, state, cache, logs, configuration, fdstore and all as values. Without --what= it removes cache and runtime data and the file descriptor store. For a unit with StateDirectory=demoapp, --what=state makes /var/lib/demoapp disappear; the next start creates it again, empty.

I like this for a cache that went bad, because you do not have to remember whether the app kept its cache in /var/cache/myapp or somewhere under a dynamic user’s private folder. systemd knows, so it removes the right thing.

Be careful with state and all. State is usually the real data of a service: databases, uploaded files, keys. It is gone for good, without a trash folder, so make a backup first. The command refuses to run while the unit is active, which at least stops you from pulling the floor out from under a running program.

Further reading: systemctl man page, systemd.exec man page

More Systemd tips [Service] sudo mkdir -p /etc/systemd/system/service.d [Service] printf 'hunter2' | sudo systemd-creds encrypt --name=dbpass – dbpass.cred systemd-analyze timespan 1h30min All Systemd tips (88) All topics

Link