Last Updated: February 25, 2016
· sluxi

Updating Drupal nodes in bulk with Drush scripts

Drush makes doing bulk operations programmatically on a Drupal site with quick snippets of PHP really easy. You can just write a bit of code and run it in a fully bootstrapped Drupal environment with "drush scr script.php". This is good for quick one-off jobs. Making it into a drush command is better for something you will use regularly and/or share with others.

As an example, I had to recently change the language of a bunch of nodes that had been created as language neutral on a Drupal 6 site. I could've used something like the languageassign module and the admin interface, but just writing a simple script was much faster. This script will set all language neutral story nodes to be in finnish instead:

$i = 0;
$sql = db_query("SELECT n.nid FROM {node} n WHERE language = '' AND type = 'story'");
while ($n = db_fetch_object($sql)) {
  $node = node_load($node->nid);
  $node->language = 'fi';

1 Response
Add your response

How would you know you're script successfully ran? Can you use drush_print in your command? Would this be logged somewhere?

over 1 year ago ·