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.

Two businessmen using computer in server room
Caching is not enabled by default in Apache.
credit: Ryan McVay/Digital Vision/Getty Images

Flushing the Cache

Step

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.

Step

Open a terminal session as the root user. Stop the Apache server by typing one of the following commands and pressing "Enter:"

Step

/etc/init.d/apache2 stop -- Debian or Ubuntu apachectl -k graceful-stop -- RHEL, CentOS or Fedora httpd.exe -k stop -n "ServiceName" -- Windows, where "ServiceName" is the name of the Apache service

Step

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.

Step

Restart Apache when the utility finishes using one of the following commands:

Step

/etc/init.d/apache2 start -- Debian or Ubuntu apachectl start -- RHEL, CentOS or Fedora httpd.exe -k install -n "ServiceName" -- Windows, where "ServiceName" is the name you assign to the Apache service

Step

Reconfigure your load balancer, proxy or other server to route requests to the server.

Periodically Cleaning the Cache

Step

Determine the cleaning interval in minutes. For example, to launch the cleaning utility every hour, the interval would be 60 minutes.

Step

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.

Step

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.