Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

Clear Cache, Swap and Temp Files in Linux

This guide explains what Linux caches are and how to clear them safely. Some steps need root access, so they fit a VPS or dedicated server.

How-To Guides2 min read9 steps

What is Linux cache?

Linux uses spare memory (RAM) to keep recently used files. This is the page cache. It makes things faster. It is normal for RAM to look full. Linux gives that memory back the moment a program needs it. So you rarely need to clear it. Do it for testing, or if you have a clear reason.

Root is the all-powerful admin account. Sudo means "run this command as the admin".

Check memory first

free -h

This shows memory in easy units. Look at the available column. That is what programs can really use.

Clear the RAM cache

  1. Write any unsaved data to disk.
    sync
  2. Drop the page cache.
    echo 1 | sudo tee /proc/sys/vm/drop_caches
  3. To drop dentries and inodes (file name info), use 2. To drop all three, use 3.
    echo 3 | sudo tee /proc/sys/vm/drop_caches

This is safe for data, but your server may run slower for a short time while the cache refills.

Clear swap

Swap is disk space used like extra memory when RAM is full. Move it back to RAM like this.

  1. Check that you have enough free RAM first with free -h.
  2. Turn swap off, then on again.
    sudo swapoff -a && sudo swapon -a
Warning: If RAM is nearly full, swapoff can freeze the server or kill programs. Do not run it in that case.

Clear temp files and package cache

  1. List the size of the temp folder.
    du -sh /tmp
  2. Delete old files in /tmp that are over 7 days old. Take a backup first if unsure.
    sudo find /tmp -type f -atime +7 -delete
  3. On Ubuntu or Debian, clear downloaded package files.
    sudo apt clean
  4. On CentOS or AlmaLinux, use sudo yum clean all or sudo dnf clean all.

Quick recap

  • A full-looking RAM is normal in Linux.
  • Run sync, then write to drop_caches to clear page cache.
  • Use swapoff -a and swapon -a only if RAM is free.
  • Clean /tmp and package caches to free disk space.