How systemd-tmpfiles Cleans Temp Files in Debian

This article explains the purpose and operation of systemd-tmpfiles within the Debian operating system, detailing how it manages, creates, and cleans volatile and temporary runtime files. You will learn the mechanics behind its configuration directories, how Debian automates the cleanup process using built-in systemd timers, and how system administrators can safely manage temporary file lifecycles.

What Is systemd-tmpfiles?

systemd-tmpfiles is a service provided by systemd that manages temporary files, volatile directories, and runtime structures. It is responsible for creating, deleting, and cleaning up volatile and temporary files and directories on a Linux system, such as those typically found in /tmp, /var/tmp, and /run.

Rather than relying on legacy shell scripts or cron jobs to clean out temporary storage, modern Debian systems use systemd-tmpfiles to enforce consistent file states, directory ownership, access permissions, and file lifespans.

Configuration Hierarchy

The behavior of systemd-tmpfiles is controlled by configuration files named with the .conf extension. Debian processes these configurations in a specific order of precedence:

  1. /etc/tmpfiles.d/*.conf: Local system administrator configuration (highest priority).
  2. /run/tmpfiles.d/*.conf: Dynamic runtime settings created by running software.
  3. /usr/lib/tmpfiles.d/*.conf: Default configurations provided by installed Debian packages (lowest priority).

If a configuration file with the same name exists in multiple directories, the file with the highest priority overrides the others.

Configuration Format

Each configuration line defines an action, target path, file mode, user, group, age limit, and optional arguments:

# Type Path               Mode User Group Age Argument
d      /run/example       0755 root root  -   -
q      /tmp               1777 root root  10d -
  • Type: Dictates the action (e.g., d to create a directory if missing, e to empty a directory, q or v for subvolumes, and r to remove files).
  • Path: The absolute path to the target file or directory.
  • Mode, User, Group: The access permissions and ownership assigned to the resource.
  • Age: The maximum retention period before an item is eligible for deletion (e.g., 10d for 10 days, 2h for 2 hours).

How Cleanup Operations Work in Debian

systemd-tmpfiles handles runtime cleanup through a combination of system boot tasks and periodic background jobs:

1. Boot Initialization

During Debian's startup sequence, the systemd-tmpfiles-setup.service and systemd-tmpfiles-setup-dev.service units run. These services parse the configuration files to re-create missing runtime directories in volatile filesystems (like /run) and apply appropriate permissions and ownership before applications attempt to use them.

2. Scheduled Cleanup

Regular cleaning of obsolete temporary files is driven by two system components:

  • systemd-tmpfiles-clean.timer: A systemd timer that activates periodically (typically once every 24 hours).

  • systemd-tmpfiles-clean.service: The one-shot service triggered by the timer that runs the command:

    systemd-tmpfiles --clean

When executed, systemd-tmpfiles --clean evaluates the Age field for each configured path. If an entry specifies an age limit, the utility inspects the access time (atime), modification time (mtime), and change time (ctime) of the files within that location. If all corresponding timestamps exceed the designated age threshold, the files are deleted.

By default on Debian, standard temporary locations such as /tmp and /var/tmp are configured via /usr/lib/tmpfiles.d/tmp.conf to remove files that have not been accessed or modified past the specified threshold (typically several days for /tmp and up to 30 days for /var/tmp).

Manual Execution and Debugging

Administrators can manually trigger temporary file cleanup or preview changes without waiting for the scheduled timer:

  • Preview changes without deleting:

    systemd-tmpfiles --clean --dry-run
  • Force immediate cleanup:

    sudo systemd-tmpfiles --clean
  • Apply creation and permission policies immediately:

    sudo systemd-tmpfiles --create

This decoupled architecture allows Debian to maintain tidy, secure runtime directories without human intervention while giving administrators fine-grained control over file retention policies.