Skip to content

Instantly share code, notes, and snippets.

@piyuesh23
Forked from ngmaloney/gist:981766
Created September 14, 2011 12:36
Show Gist options
  • Save piyuesh23/1216448 to your computer and use it in GitHub Desktop.
Save piyuesh23/1216448 to your computer and use it in GitHub Desktop.
my.drush.inc
<?php
/**
* @file My Drush Commands
*/
/**
* Implementation of hook_drush_help()
*/
function my_drush_help($section) {
switch ($section) {
case 'drush:my':
return dt("My custom drush commands");
case 'meta:my:title':
return dt('My Custom Drush Commands');
case 'meta:my:summary':
return dt('My Custom Drush Commands');
}
}
/**
* Implementation of hook_drush_command().
*/
function my_drush_command() {
$items = array();
$items['node-delete'] = array(
'callback' => 'my_drush_node_delete',
'description' => 'Performs a bulk delete operation on nodes.',
'aliases' => array('ndel'),
'examples' => array(
'drush node-delete blog' =>
'Deletes all nodes of type blog.',
),
'arguments' => array(
'type' => 'Type of node to delete. Using all will delete all nodes.',
),
);
return $items;
}
function my_drush_node_delete($node_type = '') {
if(empty($node_type)) {
return drush_set_error('Node type argument required.');
}
$query = new EntityFieldQuery;
if($node_type == 'all') {
$result = $query
->entityCondition('entity_type', 'node')
->execute();
}
else {
$result = $query
->entityCondition('entity_type', 'node')
->propertyCondition('type', $node_type)
->execute();
}
if(!isset($result['node']) || empty($result['node'])) {
$output = 'There were no nodes of type ' . $node_type . ' found.';
return drush_set_error($output);
}
$proceed = drush_confirm('You are about to delete ' . count($result['node']) . ' nodes. Proceed?');
if($proceed) {
foreach($result['node'] as $node) {
node_delete($node->nid);
}
$output = 'Successfully deleted ' . count($result['node']) . ' nodes of type ' . $node_type;
drush_log($output, 'success');
}
else {
drush_print("Node deletion cancelled.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment