Fixing Cloud Server Disk Space Issues

By Raman Kumar

Updated on Nov 21, 2024

In this tutorial, fixing cloud server disk space issues monitoring and cleanup strategies.

When managing a cloud server, one common challenge you may face is running out of disk space. This can lead to application failures, slow server performance, and unexpected downtime. To avoid these problems, it's crucial to monitor disk usage regularly and clean up unnecessary files.

This guide covers monitoring disk usage, identifying large files, and using powerful tools like ncdu and lsof to manage your server's disk space effectively. Let's dive into the step-by-step instructions to help you regain control over your server's disk space.

Fixing Cloud Server Disk Space Issues Monitoring and Cleanup Strategies

Step 1: Monitoring Disk Usage

Before cleaning up, you need to monitor disk usage to understand the problem areas. Linux provides several commands to help you check disk usage.

1.1. Using the df Command

The df command displays the amount of disk space available on file systems. Use the -h flag to display the output in a human-readable format.

df -h

Output example:

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        40G   30G   10G  75% /
tmpfs           1.9G     0  1.9G   0% /dev/shm

Key metrics:

  • Size: Total size of the disk/partition.
  • Used: Space currently used.
  • Avail: Available space.
  • Use%: Percentage of space used.

1.2. Checking Disk Usage Per Directory with du

The du command estimates the file space usage. Use it to find directories consuming large amounts of space:

du -sh /*

This command gives a summary of space usage for each directory in the root (/) directory.

Step 2: Identifying Large Files and Directories

To identify what's taking up space, you can use tools like ncdu (a disk usage analyzer) and find.

2.1. Using ncdu for Disk Usage Analysis

ncdu is a powerful command-line disk usage analyzer that provides a detailed view of space usage in a user-friendly interface.

Installing ncdu:

On Ubuntu/Debian:

sudo apt update
sudo apt install ncdu

On CentOS/RHEL:

sudo yum install epel-release
sudo yum install ncdu

Running ncdu:

Navigate to the directory you want to analyze (e.g., / for root) and run:

ncdu /

This will display a detailed list of directories and files sorted by size, allowing you to drill down to identify what's consuming space.

2.2. Finding Large Files with find

The find command is useful for locating large files. For example, to find files larger than 1GB:

find / -type f -size +1G 2>/dev/null

You can adjust the size (+1G for files larger than 1GB) to your needs. The 2>/dev/null suppresses permission-related error messages.

Step 3: Cleaning Up Unnecessary Data

Once you've identified what's taking up space, it's time to clean up. Always proceed with caution to avoid deleting critical system files.

3.1. Removing Unnecessary Log Files

Log files can quickly fill up disk space. Check the /var/log/ directory, which often contains many large log files.

View log files by size:

sudo ls -lhS /var/log/

Clearing large log files:

Instead of deleting, you can clear log files using the truncate command to avoid breaking any symbolic links or causing application issues:

sudo truncate -s 0 /var/log/large-log-file.log

3.2. Using apt and yum to Clean Up Package Caches

Package managers like apt and yum often keep caches of downloaded packages, which can take up significant space.

For Ubuntu/Debian (APT):

sudo apt-get clean
sudo apt-get autoclean
sudo apt-get autoremove

For CentOS/RHEL (YUM):

sudo yum clean all

3.3. Deleting Old Kernel Versions

Old kernel versions may accumulate and consume space. Use the following commands to list and remove old kernels:

List installed kernels (Ubuntu/Debian):

dpkg --list | grep linux-image

Remove old kernels (keep the latest two versions):

sudo apt-get remove --purge linux-image-x.x.x-xx-generic
sudo update-grub

Step 4: Using lsof to Find Open Files Consuming Space

Sometimes, deleted files still consume space if they are held open by a process. The lsof command can help identify these files.

4.1. Finding Deleted Files That Are Still Open

Use lsof to list open files:

sudo lsof | grep deleted

If you find a process holding onto deleted files, you can restart the service or kill the process to release the space:

sudo systemctl restart service_name

or

sudo kill -9 PID

Replace PID with the process ID.

Step 5: Advanced Cleanup with bleachbit (Optional)

For those who prefer a GUI tool, BleachBit can clean system caches, temporary files, and unused localizations.

Install BleachBit:

On Ubuntu/Debian:

sudo apt update
sudo apt install bleachbit

On CentOS/RHEL (requires EPEL):

sudo yum install epel-release
sudo yum install bleachbit

Run it as a root user:

sudo bleachbit

Step 6: Setting Up Disk Usage Alerts for Proactive Monitoring

Prevent future disk space issues by setting up monitoring tools. You can use duf or dstat to keep an eye on disk usage, or set up automated email alerts.

6.1. Monitoring with duf

duf is a modern and simple disk usage monitoring tool with a user-friendly interface.

Install duf:

On Ubuntu/Debian:

sudo apt install duf

On CentOS/RHEL:

sudo yum install duf

Running duf:

duf

6.2. Setting Up Email Alerts for Disk Usage with cron

Create a cron job to monitor disk space and send email alerts if usage exceeds a certain threshold.

Create a Script (/usr/local/bin/disk_alert.sh):

#!/bin/bash

THRESHOLD=80
EMAIL="your-email@example.com"

df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{print $5 " " $1}' | while read output; do
  usep=$(echo $output | awk '{print $1}' | sed 's/%//g')
  partition=$(echo $output | awk '{print $2}')
  if [ $usep -ge $THRESHOLD ]; then
    echo "Running out of space \"$partition ($usep%)\"" | mail -s "Disk Space Alert" $EMAIL
  fi
done

Make the script executable:

sudo chmod +x /usr/local/bin/disk_alert.sh

Create a Cron Job:

sudo crontab -e

Add the following line to run the script daily:

0 7 * * * /usr/local/bin/disk_alert.sh

Conclusion

By following these steps, you can efficiently monitor disk usage and clean up unnecessary files on your cloud server. Use tools like ncdu and lsof to pinpoint large files and open handles, and set up automated alerts to stay ahead of disk space issues. Regular maintenance will keep your server running smoothly and help avoid unexpected downtime.

Additional Tips

  • Schedule regular maintenance tasks to clean up temp files, old backups, and package caches.
  • Enable log rotation to avoid excessively large log files.
  • Consider a dedicated storage solution like Amazon S3 or Google Cloud Storage for large or frequently growing data sets.

This guide will help you optimize your cloud server's disk usage and keep it in top shape.

Checkout our instant dedicated servers and Instant KVM VPS plans.