How to delete error_log files from the server periodically


At times, I get inquiries for deleting error_log files from the server as they are consuming space. I strongly recommend not to delete the files. Those files contains critical information those cannot be seen through a normal debugging process. Trust me, error_log files are life saver. But, still I would like to publish a small PHP script here in order to delete the error_log files from the server.

Here is the script.

ini_set('display_errors', 0);
ini_set('log_errors', 0);
$home_folder = 'path/to/desired/folder/';
$cdi = new RecursiveIteratorIterator(
         new RecursiveDirectoryIterator($home_folder), 
$file2search = 'error_log';
foreach ($cdi as $fileinfo) {
  if ( stripos($fileinfo->getFilename(), $file2search) !== FALSE ) {

The above script will find all files starting with “error_log” (e.g. error_log, error_log.txt, error_log.txt.gz etc) and delete those from their respective locations. This script can be set as a cron job in a server where it should run once a week (may be Sunday Midnight when the server experiences lesser hit).

I hope it helps somebody and I am open for any comments.

Thank you!


I will be happy to answer your queries

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s