Drupal 7: Bulk update node url aliases

eBharatBase.com has more than 2, 60,000 nodes. We changed the pattern of url aliases to be generated for each node. We tried to generate from admin interface of pathauto module, the system hangs everytime.

The reason - query to fetch node was taking much longer to execute, and php/drupal used to show error message since they were not receiving any response.

To tackle this problem, i started writing small but effect piece of code to generate the url alises of all 2,60,000+ nodes.

1) Created script named "generate_url_alias.php" in drupal root folder

define('DRUPAL_ROOT', getcwd());
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
// menu_execute_active_handler(); // We dont need this.
error_reporting(E_ALL); // need to see all errors
$nid = variable_get("url_alias_last_nid",0);
// 5000 nodes to process each batch. If you get memory error, reduce this number
$result = db_query("SELECT nid FROM node WHERE nid > $nid LIMIT 0,5000");
foreach ($result as $record) {
$rr = db_query("SELECT source FROM url_alias WHERE source='node/{$record->nid}'")->fetchField();
if($rr == "") {
$nids[] = $record->nid;
$last_nid = $record->nid;
// generate url alises.
pathauto_node_update_alias_multiple($nids, 'bulkupdate');
} catch(Exception $e){
echo "Error generating url aliases";

This code helped in me generating the url aliases of all nodes on my website. This script is well suited for websites that need to generate url aliases for large number nodes.

If you have any comments or suggestions, please write to us.