I'm writing a module that writes several variables (three, four variables for every content type, and other).

Which method should I use to delete them on hook_uninstall?

Is it safe to perform a query, searching "mymodule_*" variables?

Or should I loop for content types and search variabels, so call many times variable_del?

  • Instead of creating variables named per content type, you could use a structured variable(s) which has a known name and keys for each content type? – Chris Burgess Dec 10 '14 at 17:55

Variables are cached, so deleting manually is fine so long as you invalidate that cache, e.g.

  ->condition('name', 'mymodule_%', 'LIKE')

cache_clear_all('variables', 'cache_bootstrap');
  • This query is OK provided that no-one has created a module which sits within your module namespace - eg if Views did this, it would vape variables belonging to modules like Views Block or Views MysiteCustom. For this example that probably wouldn't be a major issue, but you get the idea :) – Chris Burgess Dec 9 '14 at 23:13

You also might look into the Variable module which allows your module to use a registry, configure default values and so forth.

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.