Skip to main content

One post tagged with "ncdu"

View All Tags

How to find and delete files that are "eating up" space on the server

· 2 min read
Customer Care Engineer

Running out of space on your server? This may cause the site and database to malfunction. To free up space, you need to identify the files taking up the most room and delete them. In this article, we’ll explain how to easily accomplish this using the ncdu utility and how to safely clean up logs.


Step 1: Installing and running ncdu

ncdu  is a handy tool for disk space analysis. It displays all folders and files sorted by size in an easy-to-use text interface.

To use this program, you’ll need to connect to your server via SSH.  

Installation

  • Debian/Ubuntu:
sudo apt update && sudo apt install ncdu
  • CentOS/AlmaLinux/Rocky Linux:
sudo yum install ncdu

Running disk analysis

  • To scan the root directory /, execute:
sudo ncdu -x /

The -x option in ncdu restricts the scan to a single file system, excluding mounted virtual directories (e.g., /proc, /dev, /sys) and any other volumes mounted via separate mount points (e.g., network or external drives).

  • To analyze a specific directory:
sudo ncdu /path/to/directory

For example, to scan only the logs directory, run:

sudo ncdu /var/log

Step 2: Analyzing and deleting unnecessary files

After running ncdu, you will see a list of files and folders sorted by size. Navigation is simple:

  • Up/Down arrow keys — move through the list.
  • Enter — navigate into a directory.
  • D — delete the selected file or folder.

Ncdu inctruction 1

danger

Be careful when deleting system files. Delete only those files that you are sure of.

When files are deleted in Linux, they are permanently removed! Recovery is only possible through backups, and only if available.

It is safer to create a list of files and directories taking up significant space (highlight rows in ncdu and copy them to a notepad on your local PC), then review each individually and delete them using the command line.

To delete a file, run:

sudo rm -f /path/to/file

To delete a directory:

sudo rm -rf /path/to/directory

  Here's a list of the major directories that tend to take up a lot of space:

  1. /var/www/ - directory with your sites

Often, the largest directories are upload and cache in the root directory of the website, containing user-uploaded files and the site cache, respectively. For example:

/var/www/user/data/www/yoursite.com/upload/

Files in these directories are relatively safe to delete. However, only you, as the administrator of your website, know which files in the upload directory are important and which are no longer needed. It is recommended to leave the directory itself intact to avoid errors. 

  1. /var/lib/mysql/

This is the directory containing your website databases. 

danger

Please do not delete anything from this directory!

If it is taking up an unusually large amount of space, contact your hosting provider for a deeper analysis of the issue. 

  1. /var/log/

This directory stores logs generated by software running on your server. Logs have specific characteristics, and their cleanup is covered in a separate article.


Step 3: Finalizing and verifying

After deleting unnecessary files, check how much space has been freed using the command:

df -h