Deleting error_log automatically | andi setiawan's blog

I’ve been noticed by my hosting disk space warning, the indicator bar was red. Why? Oh, the disk space was decreased automatically because the error_log is growing up every my scripts executed. So, I have to delete it every time it’s growing up.


Why error_log appear automatically in my hosting directories? it is because the hosting guy has turned on the error logging, so the error on the php script won’t be displayed when it’s executed, but store the error string into a file named “error_log”.

Why there’s so many of them? Because most of the directories on my hosting has a PHP script inside it.

Why the size of them are so big for a file? Well, frankly, there are some error inside the PHP engine used by my hosting server and it is not fixed yet until now. Unfortunately, every time a script executed, the size of error_log will grow up. Even when you deleted the file, the file will remain growing up every your site loaded. So, it is important to inspect your directories, and delete error_log frequently.


The first thing I’ve done is by deleting it manually, but it will waste your time. So I created a script to do that automatically, the script also collect the size of total error_log detected.


I set the script to run once every day, you can do that by utilizing “cron job” in the cPanel (see documentation)

Here’s a sample of simple PHP cron Job:

/usr/local/bin/php -f /home/(username)/public_html/(scriptname).php


Of course I am the person who code this script, but you can do that too by using my script,.

Just copy my script in your root directory, then run it on your browser, or run it automatically thru Cron Job.


See the demo here: 

The Script

Copy and paste the script bellow to a PHP script. Or download it here.

// set the time limit first, remember, if you have 
// a huge number of files, it will be useful
$dir = "./";
echo "Looking for <b>error_log</b> in every directories <br/>";
echo "Found $num error_log files ($bytes), 
and deleted $deleted ($bytesdel) files."
echo "<hr/> <ol>$echo</ol>";
// below are the functions, modify it on your risk
// open directory, search error_log, delete it
function buka($dir){
  global $num, $deleted, $bytes, $bytesdel, $echo;
if (is_dir($dir)) {
   if ($dh = opendir($dir)) {
       while (($file = readdir($dh)) !== false) {
           if(($file!=".") and ($file!="..")){
              // cek sub dir,
              } else {                
                // kalo tipenya dir, buka..
                } else {
                // cek file error_log
                  // jumlah yg ketemu
                  } else {                
                  $echo.="<li>$filenya ($ukurannya ) </li>";
                  //kalo ketemu, delete
                  //kalo deleted, naikkan value $num
}// end function
function format_bytes($bytes) {
   if ($bytes < 1024) return $bytes.' B';
   elseif ($bytes < 1048576) return round($bytes / 1024, 2).' KB';
   elseif ($bytes < 1073741824) return round($bytes / 1048576, 2).' MB';
   elseif ($bytes < 1099511627776) return round($bytes / 1073741824, 2).' GB';
   else return round($bytes / 1099511627776, 2).' TB';

Posted in php, programming, Tips n Trik | Tagged , |

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: