Created
June 20, 2012 18:52
-
-
Save bjeavons/2961536 to your computer and use it in GitHub Desktop.
bulk delete drupal nodes of a type
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function localdev_drush_command() { | |
$items = array(); | |
$items['lcld'] = array( | |
'description' => 'Delete nodes', | |
'callback' => 'drush_lcld', | |
'options' => array( | |
'type' => 'node type', | |
), | |
); | |
return $items; | |
} | |
function drush_lcld() { | |
$type = drush_get_option('type'); | |
if (!isset($type)) { | |
drush_set_error('missing argument type', 'Pass node type to delete'); | |
return FALSE; | |
} | |
$nids = _localdev_get_nids_by_type($type); | |
$count = count($nids); | |
if ($count) { | |
$prompt = dt("Delete !count nodes?", array("!count" => $count)); | |
if (drush_confirm($prompt)) { | |
node_delete_multiple($nids); | |
} | |
else { | |
drush_print("okay, not deleting"); | |
} | |
} | |
} | |
function _localdev_get_nids_by_type($type) { | |
$nids = array(); | |
$result = db_query('SELECT nid FROM {node} WHERE type = :type', array(':type' => $type)); | |
foreach ($result as $record) { | |
$nids[] = $record->nid; | |
} | |
return $nids; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment