Why is Disk Space Management Important?
Disk space is one of the most critical resources in any shared hosting or virtual server. When disk space reaches 90% or 95%, not only does service delivery slow down, but services like MySQL or Apache may completely fail. Many users think the problem is only due to website file sizes, but in practice, old logs, extra backups, and unused caches take up a large portion of space. In this article, you will learn how to accurately check disk space using Linux command-line tools and free up valuable space through targeted cleanup.
Initial Check: What is Taking Up How Much Space?
Before any action, you need to know which areas have the highest consumption. Two basic commands are sufficient for this.
The df Command for an Overview
The df -h (human-readable) command shows the usage of each partition. A typical output looks like this:
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 20G 18G 1.5G 93% /
If the usage percentage of the root partition (/) is above 85%, it's time to act. Important note: sometimes separate partitions like /var or /home may fill up independently.
The du Command for Finding Large Folders
The du -sh /* command shows the size of each folder in the root. For a more detailed check, you can use du -sh /var/* or du -sh /home/*. A useful trick: with du -sh /var/log/* | sort -rh | head -10, you can see the ten largest files or folders in /var/log sorted.
Cleaning Old Logs (The Biggest Enemy of Disk Space)
System and service logs usually grow without limits. Files like /var/log/syslog, /var/log/messages, and Apache or Nginx logs in /var/log/apache2/ or /var/log/nginx/ can easily take up several gigabytes of space.
Removing Old Logs with logrotate
The logrotate tool is installed by default on most Linux distributions, but its settings are often incomplete. The main configuration file is at /etc/logrotate.conf. A sample configuration for Apache logs:
/var/log/apache2/*.log { weekly rotate 4 compress delaycompress missingok notifempty create 640 root adm }This setting causes logs to be rotated weekly and only keeps 4 old copies. If disk space is critical, you can set
rotate 2or evendaily.Manual Cleanup of Old Logs
If
logrotatehasn't worked properly, you can delete logs older than 30 days with the following command:find /var/log -name "*.log.*" -type f -mtime +30 -deleteYou can also remove compressed files (
.gz) withfind /var/log -name "*.gz" -type f -delete. Important note: Never delete active log files (e.g.,/var/log/syslog); only remove old versions.Managing Extra and Unnecessary Backups
Automatic backups from cPanel, Plesk, or manual scripts are often stored in folders like
/backup,/home/backup, or inside the user's folder. A common mistake: keeping daily backups for months.Finding Large Backups
You can find backup files (usually with
.tar.gzor.sqlextensions) with the following command:find / -name "*.tar.gz" -type f -size +100M -exec ls -lh {} \;If you have backups older than 14 days and disk space is low, you can delete them:
find / -name "*.tar.gz" -type f -mtime +14 -deleteCommon mistake: Users sometimes store database backups in the
public_htmlfolder, which is downloadable via the web and poses a security risk. Move these files to a folder outside web access.Cleaning System and Application Cache
Caches are designed for speed, but sometimes their size exceeds reasonable limits.
apt Cache (on Debian-based Distributions)
Packages downloaded by apt are stored in
/var/cache/apt/archives/. You can clean them with the following command:sudo apt-get cleanThis command usually frees up 200 to 500 MB of space.
MySQL/MariaDB Cache
Temporary files and MySQL binary logs in
/var/lib/mysql/or/var/log/mysql/can become large. To clean binary logs (if enabled), first log into MySQL:mysql -u root -pThen run the following command:
PURGE BINARY LOGS BEFORE NOW() - INTERVAL 7 DAY;This removes binary logs older than 7 days. Note: If you are using replication, ensure replicas are up-to-date before cleaning.
PHP Cache (WordPress and Laravel Applications)
WordPress typically stores thumbnail cache in
/wp-content/uploads/. Plugins like WP-Optimize can clean database cache and temporary files. For Laravel, check the/storage/framework/cache/and/storage/framework/views/folders.Finding Large and Unusual Files
Sometimes large files hide in unexpected folders. For example, core dump files, old logs from specific services, or files uploaded by users.
Using
ncdufor Interactive BrowsingIf
ncduis not installed, install it withsudo apt install ncdu. Then, withsudo ncdu /, you can interactively browse folders and see the largest files. This tool is much faster thandufor large directories.Finding Files Larger Than 500 MB
The following command finds all files larger than 500 MB across the entire system:
find / -type f -size +500M -exec ls -lh {} \; 2>/dev/nullReview the output and delete unnecessary files.
Managing Disk Space on Shared Hosting (Without Root Access)
If you use shared hosting and do not have SSH access, you can usually manage disk space through cPanel or Plesk. In cPanel, open the "Disk Usage" section. In Plesk, go to "Tools & Settings" > "Disk Usage". Check the
public_html,logs, andbackupsfolders. If you have SSH access, run the above commands in your home directory (without needingsudo).Preventive Tips for the Future
- Automatic Monitoring: Set up a simple script with the
df -hcommand to run daily and email you if disk space exceeds 85%. - Configure logrotate: Ensure all important services (Apache, Nginx, MySQL, PHP-FPM) have appropriate logrotate settings.
- Smart Backups: Automatically delete backups older than 30 days. Use tools like rsync to keep only the latest version.
- Limit Cache: In WordPress, configure cache plugins to occupy a maximum of 500 MB of space.
Summary
Disk space management is an ongoing process, not a one-time action. By using simple Linux commands like df, du, and find, and by configuring tools like logrotate, you can keep your hosting disk space within a safe range. If you need further guidance, the ServerNet support team can assist you. Remember: never delete system files without certainty, and always take a backup of important files before cleaning.