Apache allows you to configure cache on its web server which speeds up your website and reduces its load. However, you might need to clear your cache occasionally, if it has grown to a considerable size and consumes a lot of resources. Let us look at how to clear Apache cache. You need to stop the Apache web server, run its utility to flush cache and restart Apache. You can also schedule it to run periodically if you want.
How to Clear Apache Cache
Let us look at both the approaches to clear Apache Cache
Clear Apache Cache Manually
1. Find a Suitable Time to Clear Cache
Pick a time to clear Apache cache, when the site usage is low – during weekends/holidays/night. Set up your web server to display web pages while your website is down. Notify the users when your website will be down for maintenance.
2. Stop the Apache Web Server
Open a terminal session as root user. Stop Apache server
$ /etc/init.d/apache2 stop [Debian or Ubuntu] $ apachectl -k graceful-stop [RHEL, CentOS or Fedora]
3. Run the Apache Utility to Flush Cache
Enter the following command to start the utility. It will perform a thorough cleaning and delete sub-directories in cache folder.
$ htcacheclean -r
4. Restart Apache Web Server
Run the following command in terminal, to restart Apache web server
$ /etc/init.d/apache2 restart [Debian or Ubuntu] $ apachectl restart [RHEL, CentOS or Fedora]
Update your web server to start serving requests as before, instead of showing the “website down for maintenance” page.
Schedule to Clear Apache Cache Periodically
If you need to automate the process of flushing Apache cache periodically, then here are the steps
1. Determine the Cleaning Interval
Figure out how frequently you want to flush Apache cache. If it is a low traffic website, you can do it once every week/month. If it is a high traffic, you may have to do it every day/hour.
2. Schedule the utility
$ htcacheclean -d60 -n -t
In the above command, replace 60 with number of minutes you want for cleaning interval. ‘-n’ flag ensure that the cleaning utility pauses in between and doesn’t monopolize resources.
To stop the utility, just run the kill command for it
$ killall -9 htcacheclean
That’s it! You can clear Apache cache whenever you want, or schedule it to run periodically.