swapoff Command

The swapoff command disables swap space on Linux systems. It removes swap partitions or swap files from active use, moving any data stored in swap back to RAM. This command is essential for swap management and system maintenance.

Syntax

swapoff [OPTIONS] [DEVICE...] swapoff -a

Description

The swapoff command is used to disable swap space on Linux systems. When executed, it moves any data currently stored in the specified swap space back to RAM and then deactivates the swap device or file.

Key features:

  • Disable specific swap partitions or files
  • Disable all active swap spaces at once
  • Move swap data back to RAM before disabling
  • Support for various swap device types
  • Integration with system swap management
  • Safe handling of active swap data
Warning: Ensure you have sufficient RAM before disabling swap, as the system may become unstable if it runs out of memory.

Common Options

Option Description
-a, --all Disable all active swap spaces
-v, --verbose Display verbose output
-h, --help Display help message
-V, --version Display version information

Examples

Check current swap usage

# View active swap spaces swapon --show # Alternative method cat /proc/swaps # Check memory and swap usage free -h

Check what swap spaces are currently active

Disable specific swap partition

sudo swapoff /dev/sdb1

Disable a specific swap partition

Disable specific swap file

sudo swapoff /swapfile

Disable a swap file

Disable all swap spaces

sudo swapoff -a

Disable all active swap spaces at once

Disable swap with verbose output

sudo swapoff -v /dev/sdb1

Disable swap with detailed output

Disable multiple swap devices

sudo swapoff /dev/sdb1 /dev/sdc1

Disable multiple swap devices in one command

Temporary disable and re-enable

# Disable swap sudo swapoff -a # Perform maintenance or changes # ... # Re-enable swap sudo swapon -a

Temporarily disable swap for maintenance

Understanding Swap Management

Swap Space Types

Type Description Example
Swap Partition Dedicated disk partition for swap /dev/sdb1
Swap File Regular file used as swap space /swapfile
Swap on LVM Swap on logical volume /dev/vg0/swap
Encrypted Swap Encrypted swap partition /dev/mapper/swap

Checking Swap Status

# Show swap summary $ swapon --show NAME TYPE SIZE USED PRIO /dev/sdb1 partition 2G 0B -2 /swapfile file 512M 0B -3 # Show detailed swap information $ cat /proc/swaps Filename Type Size Used Priority /dev/sdb1 partition 2097148 0 -2 /swapfile file 524284 0 -3 # Memory and swap usage $ free -h total used free shared buff/cache available Mem: 7.7G 2.1G 3.2G 180M 2.4G 5.2G Swap: 2.5G 0B 2.5G

Different ways to check current swap status

Practical Use Cases

System Maintenance

Resize swap partition

# 1. Disable swap sudo swapoff /dev/sdb1 # 2. Resize partition (using fdisk, parted, etc.) sudo fdisk /dev/sdb # 3. Recreate swap filesystem sudo mkswap /dev/sdb1 # 4. Re-enable swap sudo swapon /dev/sdb1

Safely resize a swap partition

Replace swap device

# 1. Create new swap space sudo mkswap /dev/sdc1 # 2. Enable new swap sudo swapon /dev/sdc1 # 3. Disable old swap sudo swapoff /dev/sdb1 # 4. Update /etc/fstab sudo sed -i 's|/dev/sdb1|/dev/sdc1|' /etc/fstab

Replace one swap device with another

Performance Optimization

Disable swap for performance

# Check if you have enough RAM free -h # Disable all swap sudo swapoff -a # Remove from fstab to make permanent sudo sed -i '/swap/d' /etc/fstab # Verify swap is disabled swapon --show

Disable swap entirely for better performance (if you have sufficient RAM)

Adjust swap priority

# Disable current swap sudo swapoff -a # Re-enable with different priorities sudo swapon -p 10 /dev/sdb1 # Higher priority sudo swapon -p 5 /swapfile # Lower priority # Check priorities swapon --show

Adjust swap priorities for optimal performance

Troubleshooting

Handle swap that won't disable

# Check what's using swap sudo grep -r VmSwap /proc/*/status | grep -v "0 kB" # Find processes using swap for file in /proc/*/status; do awk '/VmSwap|Name/{printf $2 " " $3}END{ print ""}' $file done | grep -v " 0" # Force disable (use with caution) sudo swapoff -a --verbose

Troubleshoot swap that cannot be disabled

Emergency swap disable

# Check available memory free -m # Clear caches to free memory sudo sync sudo echo 3 > /proc/sys/vm/drop_caches # Try to disable swap sudo swapoff -a # If it fails, kill non-essential processes first sudo systemctl stop non-essential-service

Emergency procedures for disabling swap when memory is low

Advanced Usage

Scripting and Automation

Safe swap disable script

#!/bin/bash # Check available memory AVAILABLE=$(free -m | awk '/^Mem:/{print $7}') SWAP_USED=$(free -m | awk '/^Swap:/{print $3}') echo "Available memory: ${AVAILABLE}MB" echo "Swap in use: ${SWAP_USED}MB" if [ $AVAILABLE -gt $((SWAP_USED + 500)) ]; then echo "Safe to disable swap" sudo swapoff -a echo "Swap disabled successfully" else echo "Not enough free memory to safely disable swap" exit 1 fi

Script to safely disable swap only when sufficient memory is available

Swap management script

#!/bin/bash case "$1" in status) echo "=== Swap Status ===" swapon --show echo free -h ;; disable) echo "Disabling all swap..." sudo swapoff -a -v ;; enable) echo "Enabling swap from /etc/fstab..." sudo swapon -a -v ;; *) echo "Usage: $0 {status|disable|enable}" exit 1 ;; esac

Comprehensive swap management script

Integration with System Services

Systemd service for swap management

# Create service file: /etc/systemd/system/swap-disable.service [Unit] Description=Disable Swap After=multi-user.target [Service] Type=oneshot ExecStart=/usr/sbin/swapoff -a RemainAfterExit=yes [Install] WantedBy=multi-user.target # Enable the service sudo systemctl enable swap-disable.service

Create systemd service to automatically disable swap at boot

Best Practices

swapoff Command Best Practices
  • Check Memory First - Always verify sufficient RAM before disabling swap
  • Use Verbose Mode - Use -v option to see what's happening during the process
  • Plan for Maintenance - Schedule swap operations during low-usage periods
  • Update fstab - Remember to update /etc/fstab for permanent changes
  • Monitor System - Watch system performance after disabling swap
  • Have Backup Plan - Know how to quickly re-enable swap if needed

See also