systemd-tmpfiles on a Home Server - Give Temporary Data an Expiration Policy
Temporary data is easy to create and surprisingly hard to finish managing. A failed export can leave a half-written archive behind; a preview generator can accumulate old working files; a script may create a directory that disappears after reboot. The immediate question is often, “Where should this file go?” The longer-term question is more useful: “Who creates it, how long may it remain, and what removes it when the normal path fails?”
On a systemd-based Debian server, systemd-tmpfiles can express part of that lifecycle as configuration. Despite its name, it is not merely a command that empties /tmp. It can create paths, set selected properties, and perform age-based cleanup according to rules in tmpfiles.d. That power also makes careless experiments risky: a cleanup rule is a deletion policy, not housekeeping magic.
The policy and the executor are different things
Two names appear repeatedly in the documentation:
tmpfiles.dis the configuration format and the set of directories from which rules are read.systemd-tmpfilesis the program that applies selected operations from those rules.
The distinction matters because creation and cleanup are separate actions. According to the upstream systemd-tmpfiles(8) manual, --create applies creation and adjustment directives, while --clean processes entries that have an age policy. Running one does not silently mean the other.
Systemd normally connects these operations to boot-time services and a cleanup timer. The exact schedule and effective rules are properties of the installed system, so they should be inspected rather than assumed. On the server hosting this site, a local check found systemd 257 and an active systemd-tmpfiles-clean.timer. That is evidence about this host, not a promise about every Debian installation.
Put local policy in the local configuration directory
System rules may be supplied from several locations. For a local administrator, the important one is /etc/tmpfiles.d/. Packages normally install rules under /usr/lib/tmpfiles.d/, while runtime rules can appear under /run/tmpfiles.d/. A file in /etc/tmpfiles.d overrides a lower-priority file with the same name, as described by both the upstream tmpfiles.d(5) manual and the Debian 12 manual.
This separation has a practical benefit: package upgrades can replace vendor files without treating a local rule as package-owned data. It does not make every conflict disappear. Files are sorted by name, and multiple entries targeting the same path can conflict. Inspecting the combined configuration is therefore part of the change, not an optional troubleshooting step.
Read one rule as a lifecycle sentence
Consider a hypothetical directory used to stage article exports:
# /etc/tmpfiles.d/article-export.conf
d /var/tmp/article-export 0750 www-data www-data 2d -
The fields are type, path, mode, user, group, age, and argument. Read from left to right, this rule says: create the directory if needed, set its mode and ownership, and make sufficiently old content below it eligible for cleanup after two days. The final dash means this rule has no extra argument.
The account, path, permissions, and retention period are illustrative. A real rule should use the account that actually owns the work, a retention period derived from the workflow, and a path that contains disposable data only. A directory should not be called “temporary” merely because deleting its contents would be convenient.
d and e answer different questions
The d type creates a directory and can apply age-based cleanup to its contents. The e type operates on an existing directory and can clean its contents, but it does not create the directory. That makes e useful when another component owns path creation and tmpfiles should only express cleanup:
e /var/cache/example-preview - - - 7d -
This is not a recommendation to paste the rule onto a server. If the path contains a current job, an irreplaceable result, or files whose timestamps do not represent inactivity, the rule can be wrong even when its syntax is valid.
“Older than two days” is not just a birth date
Age-based deletion sounds simple until filesystem timestamps enter the picture. The documented default considers several timestamps for files, including modification, access, and status-change times; directories are treated slightly differently. A sufficiently recent relevant timestamp prevents cleanup. Newer systemd versions also document selectors that narrow which timestamps participate.
Two consequences follow. First, reading or modifying a file may extend its practical lifetime. Second, copied or extracted files can carry timestamps that make them look older than the operation that placed them there. The systemd project's guidance on using /tmp and /var/tmp safely discusses keeping files open, using suitable temporary-file APIs, and using BSD file locks when a temporary subtree must not be aged out while work is in progress.
Cleanup is therefore a safety net for abandoned data, not a substitute for application-level ownership. A well-behaved process should still remove its working files when it can, avoid predictable names in shared temporary directories, and handle a full filesystem. Tmpfiles helps with the failure path where normal cleanup never happens.
Inspect before applying
Start with observation. These commands do not request cleanup:
systemd-tmpfiles --version
systemd-tmpfiles --cat-config --no-pager
systemctl status systemd-tmpfiles-clean.timer --no-pager
The first establishes the available systemd version. The second prints the effective tmpfiles configuration with source filenames, which helps reveal vendor rules and local overrides. The third shows whether the cleanup timer is loaded and when it is expected to run on that machine. A missing or inactive timer is information to investigate, not a reason to run a broad cleanup command blindly.
After reviewing a new file, apply only that file's creation operation:
sudo systemd-tmpfiles --create /etc/tmpfiles.d/article-export.conf
Passing an explicit path limits this invocation to that configuration file. It still changes the filesystem, so verify the target path and ownership immediately afterward.
Dry-run support is version-dependent
Current upstream documentation includes --dry-run, but marks it as added in systemd 256. Debian 12's published systemd-tmpfiles(8) page for systemd 252 does not list that option. Check locally before relying on it:
systemd-tmpfiles --help
Where the installed version supports it, a narrowly scoped preview is useful:
sudo systemd-tmpfiles --dry-run --clean /etc/tmpfiles.d/article-export.conf
Without --dry-run, replacing that command with a real --clean is not an equivalent validation step: it can delete eligible files. On an older system, test a rule in a disposable environment or create a dedicated test path with throwaway data and inspect the installed version's manual. A backup is valuable for data, but it does not turn an imprecise deletion rule into a precise one.
When a service directive is the better fit
Tmpfiles is not the default answer for every directory needed by a systemd service. The official manuals recommend service unit directives such as RuntimeDirectory=, StateDirectory=, CacheDirectory=, and LogsDirectory= when the directory belongs to one service. Those directives keep ownership and path creation beside the service definition, and a runtime directory's lifecycle can follow the service.
Tmpfiles becomes more compelling when the lifecycle is independent of one service, when an existing shared location needs age-based cleanup, or when the required file operation is more complex than a service directory directive. This boundary also keeps the article distinct from merely provisioning /run/example: the central issue here is expiration, not just existence.
Common failure modes
- Choosing a broad parent path. A rule should target a dedicated disposable subtree, not a mixed directory whose contents have different owners or lifetimes.
- Confusing
--removewith--clean. Removal directives and age-based cleanup are separate semantics. Do not substitute one command because its name sounds similar. - Assuming age means modification time only. Access and other timestamps can influence eligibility under the default rules.
- Copying an upstream example to an older distribution. Directive modifiers and command options are versioned. Read the manual shipped for the installed release.
- Excluding files forever without assigning cleanup elsewhere. The
xandXtypes can exclude paths from age-based cleanup, but the producer then needs another reliable lifecycle. - Editing vendor files directly. Put administrator policy in
/etc/tmpfiles.dand inspect precedence instead.
A modest decision rule
Use systemd-tmpfiles when a path has a clear declarative lifecycle: what should exist, who should own it, and when abandoned content becomes disposable. Prefer service directory directives when one service owns the path and its lifecycle. Prefer application cleanup when the application alone knows whether a file is still meaningful.
The useful outcome is not “automatic cleanup everywhere.” It is a narrow policy that can be read before it runs. Temporary data deserves an expiration rule, but that rule should be treated with the same care as any other code capable of deleting files.
