9

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?

1
  • 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? Commented Dec 10, 2014 at 17:55

2 Answers 2

10

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

db_delete('variable')
  ->condition('name', 'mymodule_%', 'LIKE')
  ->execute();

cache_clear_all('variables', 'cache_bootstrap');
1
  • 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 :) Commented Dec 9, 2014 at 23:13
1

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 and acknowledge you have read our privacy policy.

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