How to Find and Delete Large Files on Your VPS or Server
Managing Full Disks on Linux
To find large files on a Linux server or VPS, you can use: find / -type f -size +1G to locate files larger than 1GB, or du -ah / | sort -rh | head -n 10 to list the top 10 directories and files consuming the most disk space.
When operating websites on a Linux VPS, regularly checking disk space prevents system crashes and HTTP 500 errors caused by a "Disk Full" status. This article guides you through the standard workflow: from checking overall usage and hunting down culprits to safely deleting massive junk files.
1. How to Check Disk Usage on Linux
Before hunting for specific files, determine which partition is actually full. Use the fundamental check disk usage command:
df -h
The -h flag outputs sizes in a human-readable format (MB, GB). Look at the Use% column to see which mount point is hitting 100% capacity.
2. Common Causes of a Full Disk
Here are the usual suspects that rapidly consume server storage:
- System Logs: Log files in
/var/log(like error.log, access.log) growing indefinitely without a Logrotate configuration. - Docker Data: The
/var/lib/dockerdirectory expanding heavily due to unused images and dead containers. - Old Backups: Forgotten
.tar.gzor.sqlbackup archives generated by plugins or manual dumps. - User Trash: The standard
rmcommand sometimes only moves files to a hidden trash folder instead of permanent deletion.
3. Methods for Finding Large Files
Method 1: Using the find Command
The find command is extremely powerful for targeting files exceeding a specific size threshold (e.g., finding files over 1GB):
find / -type f -size +1G -exec ls -lh {} ; 2>/dev/null
Note: Always terminate the -exec action with ; to prevent shell interpretation errors. The 2>/dev/null suppresses "permission denied" noise.
Method 2: Using du and sort (Top List)
To discover which directories are the heaviest, pipe the disk usage (du) output into a reverse sort (sort):
du -ah /var | sort -rh | head -n 10
Method 3: Using ncdu (Visual UI)
ncdu (NCurses Disk Usage) is highly recommended. It provides a visual terminal interface that lets you navigate folder sizes using arrow keys.
# CentOS/AlmaLinux
sudo yum install ncdu -y
# Ubuntu/Debian
sudo apt-get install ncdu -y
# Run full scan
sudo ncdu /
4. How to Safely Delete Large Files
Not all massive files are junk. Accidentally deleting system swap files or database files (.ibd) will instantly break your server.
Once you are certain about the file, use these safe deletion methods:
- Interactive Delete: Use the
-iflag to prompt for confirmation before removal.rm -i /path/to/largefile.zip - Truncate Active Logs (Safest): If the file is an active log (e.g., Nginx or MySQL), deleting it with
rmwill break the running process. Empty it instead without deleting the inode:truncate -s 0 /var/log/error.logOr the shorthand method:> /var/log/error.log
5. Common Errors (Troubleshooting)
Common hurdles sysadmins face when clearing disk space:
- "Permission denied": The scan lacks root privileges to enter deep system folders. Solution: Prepend
sudoto your find/du commands. - Scan is too slow: Scanning the entire
/takes ages. Solution: Narrow the path to suspect directories like/var,/home, or/tmp. - Deleted files but df -h isn't dropping: The file is deleted, but a running process is still holding it open. Solution: Run
lsof +L1to find the stuck process and restart the associated service.
6. Conclusion
Controlling disk space is a vital part of Linux administration. By mastering the trinity of find, du, and ncdu, you can swiftly detect and purge storage-hogging files, ensuring a smooth server operation.
It might be time to scale up. Upgrade to a Large VPS with massive SSD storage or a fully Dedicated Server to seamlessly manage big data workloads.
More Technical Guides
Explore additional technical guides and practical tutorials to optimize performance and manage your server infrastructure efficiently.