Marcio Cunha

How to clear system cache on Ubuntu via terminal safely

Learn the safe technical procedure to free up RAM memory and orphaned inodes on Linux Ubuntu servers directly from the command line.

Marcio Cunha•4 min
Also available in:PortuguêsEspañol
Summary
  • Clearing the system cache frees temporary RAM without affecting stored disk data.
  • The sync command ensures pending disk writes are safely flushed before cleanup.
  • Removing orphaned inodes prevents disk full errors when free space is available.
  • Production servers require careful timing to avoid temporary performance drops.
  • Continuous monitoring prevents unnecessary manual interventions on the operating system.

Why Ubuntu stores heavy cache in RAM memory

When you use an operating system like Ubuntu, the core system kernel stores copies of recently accessed files directly in the RAM memory. In practice, this means the next time you open a program or file, it launches instantly because reading from RAM is hundreds of times faster than reading from a traditional hard drive or SSD. This clever behavior is a core part of Linux's native performance optimization.

However, on dedicated servers running heavy applications or voracious databases, this cache can mask real resource consumption. Administrators are sometimes startled to see RAM usage near 100%, not realizing that most of that volume is just reusable cache. Before taking any drastic action, it is essential to understand that Linux automatically releases this space whenever an active application needs more memory to run smoothly.

When manual cache cleaning becomes necessary

Despite automated kernel management, there are specific operational scenarios where forcing RAM and page cache cleanup becomes indispensable. In staging environments, performance testing, and software benchmarking, for example, keeping stale data in memory can completely distort results, invalidating critical application performance metrics.

Another common scenario occurs on hardware-limited servers that have suffered extreme read and write spikes. In such cases, the system may take time to reallocate data blocks, creating hidden bottlenecks that affect essential service responsiveness. Knowing how to diagnose these moments is just as important as knowing how to diagnose CPU and temperature bottlenecks on Linux via SSH to maintain overall infrastructure health.

The critical role of orphaned inodes in file systems

Beyond RAM memory, another common ghost in busy Linux servers is inode exhaustion. In practice, an inode is a data structure that stores all information about a file or directory except its actual name and content. Every created file consumes an inode, and the system has a fixed limit defined during partition formatting, regardless of free disk space.

If your application creates and deletes thousands of tiny temporary files daily—such as web sessions, fragmented logs, or build cache files—these pointers can become orphaned or exhaust the system table. The classic symptom is the system refusing to write new files, throwing a disk full error even when the df -h command shows gigabytes of free space available on the partition.

How to prepare the Linux server for safe cleanup

Before running any cleanup commands in the terminal, the golden rule of system administration is ensuring that all pending background operations are definitively written to physical storage media. Linux manages write operations asynchronously, meaning data sent to a file may sit temporarily in memory buffers before hitting the disk.

To prevent data corruption or loss of critical information during cache release, we use the synchronization command. This command forces the system to flush all pending content from memory buffers directly to the hard drive or SSD. This step is the digital equivalent of saving your document before restarting the computer, ensuring subsequent procedures occur without risks to application stability.

Step-by-step to free memory cache and inodes

To execute the technical procedure of releasing RAM memory and page cache on Ubuntu in a controlled manner, follow the command sequence below directly in the terminal with administrator privileges. To explore these architectural concepts further in production, see our related technical analysis on How to test crystal oscillators with an oscilloscope on bench.

  1. Ensure all pending data in buffers is written to disk by executing the command
    sudo sync
  2. Clear page cache, dentries, and inodes from the system kernel by writing the value 3 to the memory control file
    echo 3 | sudo tee /proc/sys/vm/drop_caches
  3. Validate the freed memory gain by comparing current consumption with the previous state using the monitoring command
    free -h

Conclusion and best practices in resource management

Manually clearing system cache on Ubuntu via command line is a powerful tool for system administrators, but it should be treated as a surgical resource rather than an automated daily routine. The Linux kernel is engineered to manage memory and storage autonomously, and constantly interfering with this cycle can ironically hurt overall system speed by forcing constant data re-reads from disk.

Adopt these practices only during planned maintenance windows, controlled test scenarios, or real inode scarcity situations. To keep your server stable, combine these routines with continuous hardware monitoring, such as tracking disk health through the guide on how to monitor NVMe SSD health on Linux via terminal, ensuring a resilient and high-availability infrastructure.