This article applies to Drupal 7 sites on any hosting.
cache_form table on high-traffic Drupal 7 sites, can sometimes grow extremely large because the expired entries are not pruned aggressively enough. The safe_cache_form_clear module is designed to help keep this situation from ever occurring, or to help cut the table down to size once it has grown very large and has caused difficulties.
In high-traffic Drupal 7 sites, the
cache_form table can grow extremely large before it is pruned when Drupal cron runs. Additionally, the pruning during cron runs is sometimes insufficient to remove enough entries to keep the table from continuing to grow.
The module helps by providing a drush command that looks through 1000 entries at a time and deletes all entries that have passed their expiration time, which is by default 6 hours after the time they were written.
All the module does, at present, is provide this Drush command. It is necessary to set up a cron run that calls the command periodically.
The method and schedule for calling the command, and the number of entries to look through each time (defaulting at 1000) are the only parameters in the process. But the decisions in setting up the cron runs are not as difficult as one would think, because the module is very safe.
This safety stems from the fact that running the command once, results in two specific queries being run. In a simple case of deleting two expired entries from a small cache_form table with MySQL:
SELECT cache_form.cid AS cid FROM cache_form cache_form WHERE (expire <> '0') AND (expire < '1473972800') LIMIT 1000 OFFSET 0DELETE FROM cache_form WHERE (cid IN ('form_form-PvSKl0a1xHksoH6p9f_wa5FTzvXAgUqjq7ZfXZKiDg4', 'form_state_form-PvSKl0a1xHksoH6p9f_wa5FTzvXAgUqjq7ZfXZKiDg4'))
The first query looks through a number of entries in the
cache_form table, and finds all of the expired entries (greater than 6 hours past the time they were created) for the current time, which was
Wed, 21 Sep 2016 20:55:28 GMT when this example was generated. And the second query removes the expired entries.
How to prune a large
The first step when faced with a very large cache form table is running the command once, and seeing how long that takes. On Acquia Cloud the command is
time drush -d @sitename.env -l www.example.com safe-cache-form-clear
This will provide the timing of the run in the output, and if that time is much less than one minute, the recommended entry in cron is:
--any-- * * * * * /usr/local/bin/drush -d -l http://www.example.com @examplesite.prod safe-cache-form-clear
If the timing experiment did not result in much less than minute executions, your best option is to setup the cron runs to occur with a lower frequency than every minute. For example, the cron frequency string
*/10 * * * * would run the command every 10 minutes.
You might also consider running the command numerous times in a command shell until the time for each run drops much below one minute.
One can also adjust the number of entries examined for potential expiration to less than 1000 with commands like
drush -d @sitename.env -l www.example.com php-eval 'safe_cache_form_clear(80);'
Or one can more permanently adjust the number of entries with a line like
The drush command provided by the safe_cache_form_clear module is safe because it only ever removes entries that are expired from the
cache_form table. So, as long as the last command completes each time, run the command frequently.