Master Linux Disk Usage: Analyze, Clean, and Reclaim Space Quickly

Master Linux Disk Usage: Analyze, Clean, and Reclaim Space Quickly

Learn how to rapidly analyze, clean, and reclaim Linux disk usage with clear, practical commands and a simple triage workflow. Whether you’re a webmaster, developer, or admin, this guide gives the diagnostics and fixes you need to keep systems healthy and avoid full-filesystem surprises.

Managing disk usage on Linux servers is a daily reality for site operators, developers, and enterprise administrators. When disk capacity dwindles, services slow, logs stop, and deployments fail. This guide provides a technical, step-by-step approach to rapidly analyze, clean, and reclaim disk space on Linux systems. The target audience is webmasters, enterprise users, and developers who need practical commands, diagnostics, and strategies to keep systems healthy.

Understanding how Linux reports disk usage

Before cleaning, you must understand the difference between commonly used metrics and what they actually mean:

  • df shows filesystem-level usage (blocks used, available, mount point). Use df -h for human-readable output.
  • du aggregates file and directory sizes; useful for locating large directories. Example: du -sh /var/ | sort -hr.
  • inodes matter on metadata-heavy workloads. Check with df -i. A filesystem can be “full” by consuming all inodes even if bytes remain.
  • Open but deleted files: The space remains used until the file descriptor is closed. Discover with lsof +L1 or lsof | grep deleted.

Common gotcha: a file removed from directory listing still occupies space if any process holds it open. Restarting the process or truncating the FD can reclaim space immediately.

Quick analysis workflow

Use this stepwise workflow for rapid triage when you face low disk space:

  • Run df -hT to check filesystem types and mount points.
  • Use du -x --summarize --block-size=1M / or targeted du -sh /var /home /opt /usr to locate heavy directories. -x prevents crossing filesystems (helpful with /proc, /sys, or separate partitions).
  • Run ncdu / (interactive) on servers where you can install it—it’s far faster for exploring big trees.
  • Inspect systemd journal size: journalctl --disk-usage.
  • Check package caches: du -sh /var/cache/apt /var/cache/yum.
  • List Docker disk usage: docker system df and docker system prune for cleanup (careful: pruning can remove images/containers).

Examples of targeted checks

Find files larger than 100 MB:

find / -xdev -type f -size +100M -exec ls -lh {} ; | awk '{ print $9": "$5 }' | sort -k2 -h

Locate directories consuming the most space at the root level:

du -sh / 2>/dev/null | sort -hr | head -n 20

Common cleanup techniques (safe and effective)

Once you identify the culprits, apply specific strategies:

Package manager caches

  • Ubuntu/Debian: apt-get clean removes downloaded .deb packages. apt autoremove removes orphaned packages.
  • RHEL/CentOS: yum clean all or dnf clean all.

Log files and systemd journal

  • Rotate logs with logrotate and check configs at /etc/logrotate.d/. Force a rotation: logrotate -f /etc/logrotate.conf.
  • Trim journal: journalctl --vacuum-size=500M or --vacuum-time=7d to keep last 7 days.

Delete or archive old files

  • Compress old logs: gzip /var/log/old-log.log or use tar -czf archive.tgz /path/to/old/dir.
  • Move seldom-used data to external storage or backup servers before deleting.

Handle open-but-deleted files

If lsof shows large deleted files, the easiest fix is to restart the owning service. Example:

lsof +L1 | grep /var/log | awk '{print $2}' | xargs -r kill -HUP

Alternatively, truncate without restarting:

truncate -s 0 /proc//fd/

Temporary directories

  • Clear /tmp safely: find /tmp -type f -atime +7 -delete to remove files not accessed in 7 days.
  • Check /var/tmp similarly; these directories often accumulate build artifacts.

Docker and container runtimes

  • Prune unused containers, images, networks, and caches carefully: docker system prune --volumes.
  • Inspect volumes: docker volume ls and docker ps -a --filter status=exited to remove dead containers holding volumes.

Snapshots, backups, and virtual disk files

On virtualization platforms, snapshots can unexpectedly consume space. For LVM snapshots, remove obsolete snapshots with lvremove. For cloud VPS images, check the hypervisor’s snapshot management UI.

Advanced filesystem-level reclamation

When basic cleanup is insufficient, consider deeper approaches. These require care and backups.

Sparse files and hole punching

Sparse files have unallocated holes that look like disk space but may get fully allocated when overwritten. Use fallocate -d file or the fallocate “collapse range” (kernel support required) to punch holes. For example:

fallocate -p -o -l (kernel-dependent).

fstrim and filesystem-specific tools

  • On SSD-backed filesystems, run fstrim -av to reclaim blocks returned to the device—useful on thin-provisioned clouds and SSDs.
  • ext4/xfs: use filesystem-specific defrag or balance operations with caution. For XFS, use xfs_fsr for fragmentation; for ext4, mount options and periodic maintenance help.

Inode shortages

If df -i shows zero free inodes, delete many small files (e.g., caches, maildirs, tmp). Reformatting with more inodes is a last resort. Alternatively, create a new filesystem or add another partition and migrate directories with high inode usage (like /var/spool or /var/lib/mysql).

LVM, adding space, and resizing

  • Add physical extents to a volume group and extend logical volumes: pvcreate /dev/sdb && vgextend vg0 /dev/sdb && lvextend -r -l +100%FREE /dev/vg0/root. The -r flag resizes the filesystem online for many filesystems.
  • On cloud VPS, consider resizing the disk from the provider console, then grow the partition and filesystem with growpart and resize2fs or xfs_growfs.

Application-level strategies

Some systems generate bulk data—database stores, caches, media uploads, CI artifacts. Apply data lifecycle controls:

  • Set TTLs for caches (Redis, memcached) and persistence layers.
  • Prune old database backups automatically or move them off-host.
  • Configure object storage (S3-compatible) for large blobs and serve them from there instead of keeping them on the VPS.
  • Use log aggregation (ELK/EFK, Graylog) instead of storing long-term logs locally.

Choosing the right VPS offering for storage flexibility

When selecting a VPS, storage features matter as much as CPU and RAM. Consider these technical criteria:

  • Resizable block storage: Ability to increase disk size without reprovisioning minimizes downtime.
  • Snapshots and backups: Fast snapshotting and automated backup retention help recover from mistakes after aggressive cleanup.
  • SSD/NVMe performance: High IOPS reduce latency for random reads/writes—important for databases and high-concurrency web servers.
  • Separate data volumes: Attach multiple volumes and mount important directories (e.g., /var/lib/mysql) on their own partitions or LVM volumes.
  • Thin provisioning and TRIM support: Look for fstrim support and thin-provisioned storage if you rely on auto-scaling and snapshots.

Additionally, verify the provider’s guidance for resizing filesystems and snapshot lifecycle—different clouds have specific tools (cloud-init, growpart, provider APIs).

Best practices and automation

Reclaiming space once is not enough. Implement automation and monitoring:

  • Set up disk space alerts via monitoring (Prometheus + node_exporter, Datadog, Zabbix) with thresholds for used bytes and inode usage.
  • Automate periodic cleanups: configured logrotate, apt/dnf cleanup timers, and cron jobs that prune temporary build artifacts.
  • Use centralized logging and storage tiering for old or infrequently accessed data.
  • Document recovery steps: a runbook for emergency space reclamation reduces mean time to recovery.

Summary

Effective disk management on Linux combines rapid diagnostics, targeted cleanup, and long-term prevention. Start with df, du, and ncdu to find hot spots; handle package caches, logs, and tmp files first; check for open-but-deleted files; and consider container and snapshot sources. For persistent or recurring issues, add storage, use LVM, or refactor workloads to external object stores. Automate monitoring and periodic cleanup to avoid surprises.

For VPS users who need flexible, resizable storage and reliable snapshot capabilities, choosing a provider with robust disk management features simplifies many of these tasks. If you’re evaluating hosting options, take a look at VPS.DO’s USA VPS plans for details on block storage and snapshot options: USA VPS at VPS.DO.

Fast • Reliable • Affordable VPS - DO It Now!

Get top VPS hosting with VPS.DO’s fast, low-cost plans. Try risk-free with our 7-day no-questions-asked refund and start today!