Marcio Cunha

Linux disk full? How to clean logs without dropping apps

Learn how to manage disk space on Linux, perform runtime log rotation, and handle open files without interrupting your active services.

Marcio Cunha•5 min
Also available in:PortuguêsEspañol
Summary
  • Log files continue consuming disk space if their file descriptor remains open by an active process.
  • The logrotate utility automates the compression and deletion of old logs without requiring constant manual intervention.
  • The truncate command instantly frees up storage by zeroing out the file size without deleting it from the operating system.
  • Monitoring storage health prevents catastrophic failures in production servers and high-traffic environments.
  • Adjusting retention policies protects operational integrity and ensures compliance with auditing standards.

The silent danger of a full disk on Linux servers

When a Linux server reaches one hundred percent utilization on its main storage, system behavior is usually unforgiving. Databases lock up, web applications stop accepting new connections, and temporary files fail to write, triggering a wave of downtime that catches any team by surprise. In practice, this means that a lack of space blocks even the most basic read and write operations needed to keep services running.

Often, the main villain in this story is not the application code or the primary database, but the unchecked volume of event logs generated by software. These text files grow silently over days, recording every access, error, or warning. When the cleanup routine fails, the disk exhausts its capacity long before anyone notices the issue.

To understand how to resolve this situation, it helps to remember how the operating system handles storage. Linux manages files through blocks in a file system and keeps control tables to know where each piece of data is stored. When a partition fills up, the problem affects overall stability, requiring intelligent space-reclamation techniques.

In environments where resource management requires constant attention, infrastructure issues tend to appear in chains. Just as a resource shortage can freeze services or create bottlenecks similar to those observed when CPU is high in Docker, disk overflow demands precise and immediate interventions to restore operational stability without causing further damage.

Why deleting log files with rm does not immediately free up space

A very common reaction upon noticing a full disk is to open the terminal, navigate to the log folder, and run the traditional deletion command. However, in many cases, disk space remains occupied even after the file disappears from the directory listing. In practice, this happens because the process generating the log still keeps the file open in RAM.

In the Linux operating system, processes interact with files through file descriptors, which act as numerical pointers to the content on disk. When you delete a file with a simple removal command, the system only removes the name from the directory table, but the physical space remains allocated as long as a process is actively reading or writing to it.

To better illustrate this behavior, imagine you lent a book to a friend and tore the title label off the library shelf. The book remains in your friend's hand and occupies space in the real world until they decide to return it. On the server, space is only returned to the operating system when the process terminates or when the file descriptor is released.

Therefore, before blindly removing files on production servers, it is essential to check which processes are holding open descriptors to avoid unpleasant surprises and maintain the continuity of running services.

How to identify processes holding deleted files

When disk space does not reappear after deleting large files, the ideal tool to track down the issue is the lsof command, which lists open files in the system. In practice, it acts as a radar capable of scanning everything currently in use by each running program on the server.

Running the command requires administrative privileges to inspect the global behavior of the operating system. By filtering the search for files marked as deleted, you can pinpoint exactly which application is preventing the recovery of disk space.

Below is a practical example of a command used to locate these ghost files that continue consuming precious gigabytes in the background:

sudo lsof +L1

This command outputs a detailed table containing the command name, process identifier, and the size of the file that was deleted yet still has its blocks reserved. With this information in hand, the technical operator gains clarity on which service requires immediate attention.

Cleaning files at runtime with the truncate technique

If you discovered that a giant log file is locked by a critical process that cannot be restarted right now, there is an elegant alternative to reclaim space without crashing the application. This is the truncation technique, which consists of emptying the file content while keeping its descriptor intact.

In practice, using the truncate command reduces the file size to zero bytes instantly, making the operating system reclaim the allocated disk blocks without the application noticing the interruption. The process continues writing to the same file, but now starting from the beginning.

Here is how to perform this operation safely directly in your Linux server terminal:

  1. Locate the problematic log file in the corresponding directory.
  2. Run the truncation command to zero out the file without removing it.
  3. Validate the release of disk space using your monitoring utility.

The practical command to perform this operation safely is presented in the block below:

sudo truncate -s 0 /var/log/your-application/app.log

This approach ensures that the system continues to run perfectly without the application crashing or needing to go through a time-consuming restart during peak hours.

Automating log rotation to prevent future issues

The best strategy to prevent the disk from filling up again is to implement an automated management and rotation policy for log files. The standard utility for this task in Linux-based operating systems is logrotate, which handles compression, archiving, and scheduled deletion.

Configuring logrotate involves creating specific rules for each type of application, determining how often logs should be closed, how many histories should be kept, and whether compressed files should be sent to external long-term storage.

A typical configuration file defines clear guidelines for system behavior, ensuring that log growth is controlled autonomously and predictably over the weeks.

Below is a functional example of a logrotate configuration file to manage application logs efficiently:

/var/log/my-app/*.log {
daily
rotate 7
compress
delaycompress
missingok
notifempty
copytruncate
}

Using the copytruncate directive in the configuration block above is especially useful for software that lacks a native mechanism to close and reopen log files when receiving a system signal, ensuring rotation without downtime.

Final considerations on server storage health

Keeping a Linux server's storage under control requires operational discipline and the correct use of native operating system tools. Understanding the difference between removing a file and releasing its active descriptors avoids critical situations in production environments where stability is the top priority.

Investing time in setting up automated routines and preventive monitoring saves hours of work on emergency calls. With well-structured processes, your infrastructure remains resilient, ensuring that data growth never turns into an obstacle to the success of your projects.