How to Flush Apache's Cache
If you configure and enable caching on an Apache Web server, you can use an Apache utility to flush or clean the cache. To completely flush the cache, you must stop the Web server, run the utility and then restart the Web server. Alternatively, you can schedule the utility to run on a periodic basis to keep the cache clean. You might have to experiment to determine the best interval to use for cleaning based on how long it takes to run the utility and the typical size of the cache.
Flushing the Cache
Select a time when you can stop the Apache Web server to flush the cache. Configure your load balancer, proxy or other server to serve Web pages while the server is down or to notify users that the server is down for maintenance.
Open a terminal session as the root user. Stop the Apache server by typing one of the following commands and pressing "Enter:" /etc/init.d/apache2 stop -- Debian or Ubuntu apachectl -k graceful-stop -- RHEL, CentOS or Fedorahttpd.exe -k stop -n "ServiceName" -- Windows, where "ServiceName" is the name of the Apache service
Type "htcacheclean -r" (without the quotation marks) and press "Enter" to launch the cache cleaning utility, perform a thorough cleaning and delete subdirectories during the cleaning process.
Restart Apache when the utility finishes using one of the following commands:/etc/init.d/apache2 start -- Debian or Ubuntuapachectl start -- RHEL, CentOS or Fedorahttpd.exe -k install -n "ServiceName" -- Windows, where "ServiceName" is the name you assign to the Apache service
Reconfigure your load balancer, proxy or other server to route requests to the server.
Periodically Cleaning the Cache
Determine the cleaning interval in minutes. For example, to launch the cleaning utility every hour, the interval would be 60 minutes.
Open a terminal session as the root user. Type"htcacheclean -d60 -n -t" (without the quotation marks) and press "Enter" to launch and schedule the cleaning utility. Replace "60" with the number of minutes you selected for the cleaning interval. Omit the "-n" flag if you don't want the cleaning utility to pause occasionally so it doesn't monopolize system resources.
Type "killall -9 htcacheclean" (without the quotation marks) to stop the daemon in Linux or type "net stop htcacheclean" (without the quotation marks) to stop the service in Windows.
Tips & Warnings
- Review the system log to determine the amount of time it takes for the cleaning utility to run and adjust the cleaning interval as necessary.
References & Resources
- Apache HTTP Server Documentation: htcacheclean -- Clean Up the Disk Cache
- Apache HTTP Server Documentation: Stopping and Restarting
- NixCraft: Start/Stop/Restart Apache2 Web Server
- Apache HTTP Server Documentation: Using Apache HTTP Server on Microsoft Windows
- NixCraft: Kill Process in Linux or Terminate a Process in Unix/Linux Systems
- Microsoft TechNet: Start, Stop, Pause, Resume or Restart a Service
- Apache HTTP Server Documentation: Caching Guide