Linux Swap Space Full: How to Clear Swap Without Rebooting Server
Quick answer
Full swap space can be cleared without a reboot by temporarily disabling and re-enabling it — this forces the kernel to move any swapped-out data back into...
Full swap space can be cleared without a reboot by temporarily disabling and re-enabling it — this forces the kernel to move any swapped-out data back into physical RAM (if there's room) or fail safely if there genuinely isn't, giving you a clean, predictable way to reset swap usage while the system stays up.
The Problem
Swap usage has grown very high, sometimes reaching full or near-full capacity, and you want to clear it out without rebooting the entire server:
free -h
total used free
Swap: 4.0Gi 3.9Gi 0.1Gi
A full reboot would work, but is disruptive for a production server, particularly if the actual application processes are still running fine despite the high swap usage.
Why It Happens
Swap fills up when the kernel moves memory pages from physical RAM to disk-based swap space, typically because physical RAM became full or the kernel's own memory management heuristics decided moving certain pages to swap was beneficial for overall system performance at some point. High swap usage isn't inherently a problem — it's a normal part of how Linux manages memory — but it can become a concern if:
- Swap remains heavily used long after the memory pressure that originally caused it has subsided, since Linux doesn't automatically move data back from swap to RAM just because RAM later becomes available again — data tends to stay in swap until something specifically needs it
- Swap usage itself is now causing performance problems, since accessing swapped data (on disk) is dramatically slower than RAM, and processes that need frequently-accessed data that's ended up in swap experience real, sometimes significant slowdowns
- Swap is genuinely running low on available space, risking future memory allocation failures if new memory pressure occurs with no remaining swap capacity to absorb it
The Fix
First, confirm you have enough free physical RAM to actually hold the data currently in swap — clearing swap moves that data back into RAM, and attempting this without sufficient free RAM available can cause the system to immediately swap again, or in severe cases, trigger out-of-memory conditions:
free -h
# confirm "free" RAM comfortably exceeds current swap "used" amount
If you have sufficient free RAM, disable and immediately re-enable swap, which forces the kernel to move all swapped data back into RAM as part of the disable operation:
sudo swapoff -a
sudo swapon -a
swapoff -a disables all configured swap space, and as part of doing so, the kernel must move any data currently in swap back into RAM (since it can no longer live in the now-disabled swap) — this is the actual mechanism that clears swap usage. swapon -a immediately afterward re-enables swap, restoring it to an available, but now empty, state for future use if needed.
If you have multiple swap devices/files and only want to clear one specifically rather than all of them at once, target it individually:
sudo swapoff /swapfile
sudo swapon /swapfile
Monitor the operation, since moving a large amount of data from swap back to RAM can itself take noticeable time and briefly increase system load while it's happening:
watch -n 1 free -h
If you don't have sufficient free RAM to safely clear swap this way, don't force it — attempting to disable swap without enough RAM to receive the data can cause serious system instability. Instead, address the underlying cause of high memory usage first (identify and potentially restart or optimize whatever process is consuming excessive memory), and only attempt to clear swap once there's genuinely sufficient free RAM available to safely absorb it.
After clearing swap, investigate why it filled up in the first place, since simply clearing it doesn't address the underlying cause, and it may well fill up again if that root cause remains unaddressed:
ps aux --sort=-%mem | head -10
A specific process consistently consuming excessive memory, a genuine memory leak, or simply insufficient physical RAM for your actual workload are all common underlying explanations worth investigating and addressing directly, rather than only periodically clearing swap as a recurring workaround.
Still Not Working?
If swapoff -a hangs or takes an extremely long time without completing, this can indicate the system genuinely doesn't have enough free RAM to accommodate the swapped data, and the kernel is struggling to complete the operation — in this specific scenario, forcibly interrupting the swapoff command isn't necessarily safe either, and the more prudent path is often to let it continue (even if slow) while closely monitoring available memory, or, if the system becomes genuinely unresponsive, planning for a controlled reboot during a maintenance window rather than continuing to force an operation the system doesn't currently have adequate resources to complete cleanly.