A malfunctioning PHP script or extension can write thousands of warnings to the log every minute, and the error_log files generated in the website’s directories can grow to gigabytes in size within a few days. This article explains how to find them using the find command, how to free up space without causing further errors, and what to do to prevent them from building up again.
We tested the commands on an AlmaLinux 10.2 test machine, using a sample directory. Run them in the website folder (for example, ~/public_html); the . always refers to the current directory.
On aWh’s cPanel hosting plans, PHP logs to the logs/php.error.log file in the home directory by default; therefore, error_log files are generated less frequently in website folders. However, this is common with older configurations, on other servers and with certain applications.
1. List of error_log files by size
find . -type f -name error_log -exec du -sh {} +
-type file: searches for files only;-name error_log: only files named exactlyerror_log(-inamesearches regardless of case);-exec du -sh {} +:duprints the sizes of the files found. The{} +passes the files to a singleducall, which is faster for large numbers of files than{} \;(one call per file).
The total size of all error_log files:
find . -type f -name error_log -print0 | du -ch --files0-from=- | tail -1
2. Only the big ones: the -size switch
find . -type f -name error_log -size +100M -exec ls -lh {} \;
The -size +100M option applies to files larger than 100 MB. Be careful with the unit of measurement when dealing with gigabytes:
-size +1Grefers to files larger than 1 GiB (1,073,741,824 bytes);-size +1000000000c(wherecstands for bytes) refers to files larger than 1 billion bytes, which is slightly less.
In the test, a 1010 MB error_log file was found by the -size +1000000000c option, but not by the -size +1G option; a 1.1 GB file was found by both. The find command rounds up, so the -size +1G option does not yet find the 1 GiB file, only the larger one. For a more precise limit, specify it in megabytes: -size +1024M.
3. Freeing up space: emptying instead of deleting
Do not delete (rm) a log file that is currently in use; instead, clear it:
find . -type f -name error_log -size +100M -exec truncate -s 0 {} +
The truncate -s 0 command truncates the file to zero bytes; the file itself remains intact. If a running program (the web server, PHP-FPM) currently has the log file open, the space occupied by the deleted file will not be freed up until the program restarts: according to df, the disk will still appear full, whilst the file will no longer be visible anywhere. This phenomenon and its solution are described in the article ‘The disk is full, but du doesn’t show it’.
If you do wish to delete it, however, because you are certain you will not be using it for anything (for example, if it is in the folder of an old application that no longer exists):
find . -type f -name error_log -size +100M -delete
Deletion is permanent; there is no recycle bin. Always check the list without the -delete option before running the command.
4. Before you clear it: check what’s in it
A large error_log is a symptom, not a cause: the same error will fill it up again within a few days. The last few lines show which file and which line are causing the problem:
tail -n 20 error_log
A summary of the most common messages (without dates):
cut -d']' -f2- error_log | sort | uniq -c | sort -rn | head
If most of the lines are PHP Deprecated or PHP Warning, the PHP version and the plugin version are not compatible: update the plugin or the theme. If there is a PHP Fatal error, part of the website is not working at all. For WordPress, the article ‘WordPress debug mode (WP_DEBUG)’ explains how to troubleshoot.
5. To prevent regrowth
- Correct the error that is generating these lines (see the previous point).
- Do not display or log warnings en masse on a live website: in the
php.inior.user.inifile, seterror_reporting = E_ALL & ~E_DEPRECATED & ~E_NOTICEto retain only critical errors (see: Using the .user.ini file to configure PHP parameters). - On your own server, the system logs are rotated and compressed by
logrotate; this does not apply to theerror_logfiles of websites, unless you specify a separate rule for them.
For information on checking total disk usage, see the article introducing the du and df commands; for cPanel hosting, see the article ‘Checking disk usage in cPanel’.