Marcio Cunha

How to clear temporary files on Linux with tmpfiles

Learn how to configure systemd-tmpfiles to automate the safe removal of temporary files and caches on Linux, reclaiming disk space permanently.

Marcio Cunha•3 min
Also available in:PortuguêsEspañol
Summary
  • The systemd-tmpfiles utility automates temporary directory cleanup natively and securely on Linux.
  • Configuring custom rules prevents unnecessary cache buildup inside user home directories.
  • Proper command usage prevents accidental deletion of important working files in production environments.
  • Efficient space management complements other maintenance routines such as clearing system logs.
  • Automation reduces the need for constant manual intervention in infrastructure maintenance.

Why does Linux accumulate invisible cache and digital clutter?

Anyone using Linux-based operating systems for a while notices that the hard drive slowly loses free space over time. Messaging applications, web browsers, and development tools constantly create ephemeral working files that end up forgotten in hidden system directories. In practice, this behavior consumes valuable gigabytes that could otherwise be used for important data.

To work around this issue without relying on external utilities, modern distributions feature the native systemd-tmpfiles utility. This tool is integrated directly into the operating system's initialization manager, monitoring specific directories and executing cleanup routines based on policies configured by the administrator, working similarly to what we saw when learning how to clear Systemd cache and logs on Linux via terminal.

Understanding the role of systemd-tmpfiles in system routines

Unlike manual scripts running at fixed times via cron schedulers, systemd-tmpfiles operates intelligently during computer startup and at regular subsequent intervals. It reads configuration files distributed across standard system paths and applies strict rules regarding expiration, age, and file type, ensuring no essential data is accidentally deleted.

This structured approach eliminates the need for third-party tools that often run in the background consuming unnecessary resources. Furthermore, managing temporary directories complements other essential operating system maintenance routines, such as cleaning orphaned files as discussed when exploring how to clean orphaned files on Linux securely with BleachBit.

How to create custom rules for user home directories

By default, the system only cleans global directories like /tmp and /var/tmp. However, it is entirely possible to extend this automation to each user's home directory, cleaning browser caches, local log files, and old downloads that are no longer needed. To achieve this, we create custom configuration files inside the user configuration folder.

The process requires creating a configuration file with a .conf extension inside the ~/.config/tmpfiles.d/ directory. Inside this file, we define the operation type, the absolute path of the directory, access permissions, the owning user, and the maximum age a file can reach before being permanently removed by the system tool.

Step-by-step guide to setting up automatic user cleanup

To put this strategy into practice safely and in a controlled manner, follow the procedure below directly in your terminal:

  1. Open your terminal and create the local configuration directory if it does not exist yet using
    mkdir -p ~/.config/tmpfiles.d
  2. Create a custom configuration file using your preferred text editor via
    nano ~/.config/tmpfiles.d/user.conf
  3. Add the desired rule specifying the path and retention time, for example
    e /home/user/Downloads - - - 30d
    and save the file.

Testing and validating your rules before actual execution

Before letting the system run cleanup autonomously, it is essential to test whether the syntax is correct and check which files would be affected by the newly created rule. The systemd-tmpfiles utility has a very useful simulation mode that displays on screen all operations that would be performed without actually deleting any data from the disk.

To run this check, we use the command with specific debugging and listing parameters. This precaution prevents unpleasant surprises and ensures poorly written rules do not exclude recent working files, maintaining the integrity of both your development environment and daily use. To explore these architectural concepts further in production, see our related technical analysis on How to test a MOSFET on board with a digital multimeter.

Final considerations on cleanup automation

Keeping your computer storage organized and free from obsolete files stops being a manual task and becomes completely transparent when entrusted to native systemd tools. By configuring custom rules for user directories, you gain total control over the lifecycle of temporary data, ensuring an operating system that remains fast, responsive, and with plenty of space for what truly matters.