How to Flush Apache's Cache

By Steve McDonnell

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

Step 1

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 2

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

Step 3

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 4

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

Step 5

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

Periodically Cleaning the Cache

Step 1

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

Step 2

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 3

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.