Why Deleting Apache Logs Doesn’t Free Disk Space and How to Fix It
This guide explains why removing Apache/Tomcat log files while the services are running fails to release disk space, analyzes the inode‑locking behavior, and provides practical solutions such as restarting the server, truncating logs, and using scheduled cleanup commands.
1. Scenario : A web server running Apache/Tomcat reports full disk space with df, yet after deleting log files the space remains full; du -sh /* shows the total file size far below the disk capacity.
2. Problem analysis : Deleting logs while Apache/Tomcat is active does not free space because the running process still holds the log file’s inode. The file is merely unlinked, so the allocated blocks stay occupied.
3. Solution : Restart Apache/Tomcat, or truncate the log file without deleting it.
4. Extended – better log‑deletion methods :
(1) Delete logs older than three days via a scheduled task, e.g.:
find /data/www/tomcat-webserver/logs/ -type f -mtime +3 | xargs rm -f find /data/www/tomcat-webserver/logs/*log* -type f -mtime +3 | xargs rm -f find /data/www/tomcat-webserver/logs/ -type f -mtime +3 -exec rm -f {} \;(2) Clear the current log without removing the file:
cat /dev/null > ./logs/test.log > ./logs/test.log5. Production simulation :
Install Apache and modify the log path:
yum install httpd sed -i 's#CustomLoglogs/access_log combined#CustomLog /app/logs/access_log combined#g' /etc/httpd/conf/httpd.confCreate a large file to simulate a small‑capacity disk:
dd if=/dev/zero of=/mnt/apachelog bs=10K count=10 mkfs.ext4 /mnt/apachelogMount the loop device to the log directory:
mkdir -p /app/logs mount -o loop /mnt/apachelog /app/logs/Start Apache and generate heavy traffic to fill the disk:
/etc/init.d/httpd start for i in $(seq 10000); do curl -s http://192.168.1.200; doneSimulate normal log deletion:
rm -f /app/logs/access_log df -hThe df -h output shows the filesystem still reports high usage, and lsof | grep access_log reveals processes (httpd) still holding the deleted log file.
6. Final resolution :
(1) Restart Apache to release the inode: /etc/init.d/httpd restart (2) Truncate the log file directly:
> /app/logs/access_logSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
