How To Delete All The Script Error Logs In Ubuntu And CentOS Using A Single Command?

TechHow ToHow To Delete All The Script Error Logs In Ubuntu And CentOS...

Error logs are invaluable tools for diagnosing and troubleshooting issues in software applications and systems. They provide insights into unexpected behaviors, crashes, and errors that occur during the execution of programs. In my previous article, I wrote about how to enable PHP error logs reporting in the same folder where the script is. Having error logs reported in the same folder where scripts are executed brings several advantages. When an error occurs, the corresponding error log is created in the same directory, maintaining a clear association between the error and the script that triggered it.

However, over time, these logs can accumulate and consume significant disk space. In this tutorial, we will explore how to efficiently delete all error log files from a directory and its subdirectories(recursively) using a single command in Ubuntu and CentOS.

Why Are Error Logs Generated?

Error logs are generated when software applications encounter unexpected situations, exceptions, or errors during their execution. These situations could include invalid inputs, unexpected system behaviors, or issues related to external dependencies. Error logs capture relevant information about the error, such as timestamps, error messages, stack traces, and other contextual data. Analyzing these logs helps developers and system administrators pinpoint the root causes of problems, facilitating faster issue resolution.

Importance of Error Reporting

Error reporting is a critical aspect of software development and system administration. It provides insights into the stability and health of applications, helping teams address issues proactively. Some key benefits of error reporting include:

  1. Issue Identification: Error logs highlight potential problems, enabling developers to address them before they escalate.
  2. Faster Debugging: Detailed error information expedites the debugging process, as developers can quickly pinpoint the problematic code.
  3. Performance Optimization: Identifying recurring errors allows teams to optimize application performance and stability.
  4. Historical Tracking: Error logs serve as historical records, aiding in the understanding of past issues and their resolutions.

The Command to Delete All Error Log Files from a folder and all of its subfolders recursively

Important Note: Be careful when removing log files. Not all logs are the same. Some, like SQL bin logs, a few system logs, and Apache error logs, are super important for key web services like databases and web servers. If you delete them, things like servers might stop working or crash and cause big problems. These log files are required to be present, though their contents can be cleaned or emptied out but the absence of these files may cause service disruption. Unlike logs from scripts (like PHP or Python), these special logs won’t come back on their own. If deleted by mistake, you will need to create those log files again with the exact same name, otherwise, the server will stop throwing a file not found error. So, before you delete any logs, know what they do. Save the really important ones, and keep things tidy by checking logs regularly. Make sure important stuff keeps working well while you clean up.

So here we are only talking about the error logs generated by scripts such as PHP or Python, these log files can be automatically generated if not found, and do not cause any service disruption. They are safe to delete and help save disk space.

To delete all error log files from a directory and its subdirectories recursively in Ubuntu and CentOS, you can use the find command combined with the rm command. Open a terminal and navigate to the directory where you want to delete the error logs. Then, execute the following command:

First, find and list all the logs to verify you are deleting all the script error logs.

find /var/www/ -type f -name "php_errors.log"

In the above example, I knew the PHP error log file name, so each folder will have this file name in my web root directory (/var/www). So, I verified the list of files and I am ready to delete them now.

Find and List All Script Error Logs in Ubuntu and CentOS
Find and List All Script Error Logs in Ubuntu and CentOS

Now run this command to delete them all.

find /path/to/directory -type f -name "error*.log" -exec rm {} \;
  • Replace /path/to/directory with the actual path of the directory from which you want to delete the error logs. In our case it was /var/www.
  • The -type f flag specifies that the search should only consider regular files.
  • The -name "error*.log" flag filters files with names starting with “error” and ending with “.log”. In our case, we could type php_errors.log as the file name to find and delete.
  • The -exec rm {} \; flag executes the rm command for each found file.

The command should delete all the PHP error logs quickly from the folder and all of its sub-folders.

Does this command differ in different Linux distributions?

The find command is available on most Linux distributions, and its basic usage remains consistent across different versions including popular Linux distributions, like Fedora.

Can I safely delete all log files on my system to free up space?

A: While it’s important to manage log files for disk space efficiency, not all logs can be deleted without consequences. Certain logs, such as SQL bin logs and Apache error logs, are crucial for core web services. Deleting these logs can lead to service disruptions and even system crashes. Always identify the purpose of each log before deletion and focus on non-essential logs to free up space.

What’s the difference between logs generated by scripts and logs from essential services?

Logs generated by scripts, like PHP or Python scripts, are primarily for developers to identify errors within the script’s execution. These logs can be safely deleted as they regenerate when the script runs again. On the other hand, logs from essential services like databases and servers (e.g., SQL bin logs, Apache error logs) are integral to their proper functioning. Deleting them can impact service stability and may require manual intervention to restore normal operation.

Can I automate the log file deletion process?

Yes, you can automate log file deletion using tools like cron jobs or scheduled tasks. Create a script that includes the find command with appropriate options and run it periodically. However, ensure your script excludes logs critical for service operation. Regularly review and adjust the script as needed to maintain a balance between log management and service stability.


Managing error logs is an essential practice for maintaining robust software applications and systems. In this tutorial, we explored a simple yet powerful command to delete all error log files from a directory and its subdirectories recursively in Ubuntu and CentOS. We also discussed the reasons behind error log generation, the advantages of configuring separate error log files, and the convenience of having error logs reported in the same folder as the scripts. Error reporting remains a cornerstone of proactive issue resolution and performance optimization, making it an indispensable tool for developers and system administrators alike. By following these practices, you can streamline error management and create more reliable software systems.

If you are having issues with low server disk space on your VPS, and you worry about deleting these log files and can not find the cause of high disk usage or don't know which files taking up so much space. In such cases, you can contact us through Skype at live:hawkdivetech and we can provide assistance.

Nasir Sohail
Nasir Sohail
Nasir is a software engineer with an M.Sc. degree in software engineering and various certifications related to computer hardware and networking, such as MCSE, CCNA, RHCE. He has more than 15 years of mixed industry experience mostly related to IT Support, Web development and Server administration. He also offers his freelancing gig for IT support and consultancy and has more than 300 five-star reviews across platforms like Fiverr, Google, TrustPilot, etc.
Watch & Subscribe Our YouTube Channel
YouTube Subscribe Button

Latest From Hawkdive

You May like these Related Articles


Please enter your comment!
Please enter your name here

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