I am working on a Drupal 7 site. When I check the cache_form table it reaches 4 GB daily so I have to manually truncate the cache_form table. Is there any way to clear cache_form table automatically 4 times in a day?

  • 1
    do you have drush ? or do you ssh access to your server?
    – Yuseferi
    Mar 15 '17 at 12:32
  • 1
    I have cPanel access.
    – drupal
    Mar 15 '17 at 12:54
  • 1
    You need to figure out why {cache_form} is getting so big. Just pruning it is just fixing a symptom; you need to find the underlying cause. It sounds like you have an anonymous form (contact forms, comments, exposed views forms, etc) and you have a lot of bot activity on these pages. Bad Behavior or explicit IP blocking may be needed.
    – mpdonadio
    Mar 15 '17 at 15:29

There are several method to clear the cache_form automatically.

Use optimizedb module

There is well-known issue with fast-growing table cache_form. This table stores Drupal form cache and it isn’t cleared when you flush all cache or run cron. Also you can struggle with problem of fragmentation with InnoDB tables on active delete/insert operations. This module provide functionality to solve this problem. Also its provide additional tools for maintenance database.

Module OptimizeDB provide such features:

Ability to clean **cache_from** in administrative page or do it by cron.
Ability to optimize all database’s tables and display its sizes.
Configuration of notification about necessity to perform maintenance tasks.
Perform check and repair operation with tables.
Prevent crashing tables when perform all maintained actions.

If you have drush access Safe cache_form Clear module

Safely remove a limited number of items from the cache_form table.

This module provides a drush command drush safe-cache-form-clear that can be run at a high frequency, keeping the table at a reasonable size. It requires that you use the database cache back-end for the cache_form table.

Drupal's core cron can have problems clearing out the cache_form table when it grows extremely large. This module works around this by limiting the number of items expired from the cache in a single run, dealing with a manageable chunk each time.

You can configure the number of items deleted in a single run by setting the $conf['safe_cache_form_clear_limit'] persistent variable.

for automatically clear the Drupal cache use Cleaner module (it's not clear the cache_form table).

The Cleaner module allows the admin to set a schedule for clearing caches, watchdog, and old sessions.

and another solution is create a custom route in your module something that not predictable by others (some hash url), on call back function truncate 'cache_form' table (or equivalent Drupal ) then on your server or your Cpanel create a crontab,schedule it to run the code periodically.

  • 1
    Thanks, Is there a way to stop the form being cached completely?
    – drupal
    Mar 15 '17 at 12:53
  • 1
    You actually need some minimal amount of cache for forms.
    – Kevin
    Mar 15 '17 at 12:58
  • 1
    @drupal there is some hacky solution but it's strongly recommended you don't do that..
    – Yuseferi
    Mar 15 '17 at 12:58
  • 1
    @drupal optimizedb is awesome and can do magic for big databases.
    – Yuseferi
    Mar 15 '17 at 12:59
  • 2
    What do you mean reduce? If you are confident about your setup, you can write a simple hook_cron to delete records with timestamps older than foo from cache_form. But it really isn't advised. Anything lower than 30-60 minutes is too low IMO.
    – Kevin
    Mar 15 '17 at 13:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.