Best practice to disable a module by code

mburak's picture

Hi, I was just thinking about how it would be if I need to add a new functionality to my site that includes uninstalling a module.
I don't want to do it by database because I need to have all my configurations in code (and in svn).

What would you recommend?




mrchrisadams's picture

Hi Matias,

I've had a similar problem, too and in the end the only approach that worked reliably for me was to to use the database, and update_sql.

Sucks, I know.

Here's an example of installing modules (with the extra code needed that needed to be called as well as the install_modules function

function project_update_6207() {
$ret= array();
// activate our modules
drupal_install_modules($module_list = array('permissions_api', 'secure_permissions', 'project_stored_permissions'));

// installing modules does not actually install the modules. We actually need to
  // use an sql query too.

update_sql("UPDATE {system} SET status = 1 WHERE name = 'project_stored_permissions'");
// without this our permissions aren't actually set
variable_set("secure_permissions_active", TRUE);

$ret[] = array(
'success' => true,
'query' => 'Enabled version controlled permissions',

I couldn't find an equivalent for disabling modules, so I ended up resorting to database calls there too.

Here's a snippet that worked for me, when I was pushing some new code on a deploy, to disable a module called context_contrib:

function project_update_6203() {

$ret= array()

update_sql("UPDATE {system} SET status = 0 WHERE name = 'context_contrib'");

$ret[] = array(
'success' => true,
'query' => 'Disabled context contrib'

I'm using the $ret array to display some kind of feedback, and really there should be some kind of checking to make sure that update_sql returns true. But this should give an idea on how to switch modules off if you need to through code.

I'm basing this on Austin Smith's own blog post on the same subject


colan's picture

If Drush is an option, simply do this:
"drush @<site-alias> dis <module-name>"

You can stick that in a script controlled by your version control system.

Thanks for your answers. I

mburak's picture

Thanks for your answers. I know how to do it (@mrchrisadams isn't module_disable function working for you?) but I was talking more about concepts. Where should I do this? In a module called project where I do all the updates to the project? In a script?
Then it comes another question: Can I disable and delete a module in the same deploy? I guess not, I think that I have to disable it first (like deprecating it) and delete it in the next version.
Waiting for comments...


Check out the Environment

patcon's picture

Check out the Environment module:

Sounds like that's exactly up your alley :)

@mrchrisadams, and yeah, these should do the trick, if I'm understanding correctly:

Also, you mentioned that drupal_install_modules doesn't actually install [enable?] modules, but it ends with a call to module_enable(), so as far as I can tell, it should...

use drush drush

Ilya1st's picture

use drush
drush pm-disable
drush pm-uninstall

by the way this is better solution to avoid php script time limit in the webserver.

Packaging & Deployment

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: