Skip to content

Instantly share code, notes, and snippets.

@bdombro
Last active August 29, 2015 14:01
Show Gist options
  • Save bdombro/d90b20c7a0915766b18c to your computer and use it in GitHub Desktop.
Save bdombro/d90b20c7a0915766b18c to your computer and use it in GitHub Desktop.
Drush Delete Nodes in Mass Script
<?php
// ************ This script can be run from drush, like `drush scr <name of script>`
print "\n*************** Delete Nodes en mass V1.1 *******************";
print "\nThis program will delete nodes en mass.\nBe very careful when configuring to not accidently delete nodes.\n";
global $count1;
global $total;
$count1 = 0;
$total = 0;
// ********** Catch CTRL-C
declare(ticks = 1);
pcntl_signal(SIGINT, "signal_handler");
function signal_handler($signal) {
switch($signal) {
case SIGINT:
global $count1;
global $total;
print "\n\nSIGINT caught.\nDeleted ".$count1 ." / ". $total ." nodes\n";
die(0);
}
}
// ********** Execute
// $nids = db_query("SELECT nid FROM node WHERE type = 'page' "); // not as good
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
->entityCondition('bundle', ['accident','river','river_section','river_feature','project'], 'IN')
//->propertyCondition('status', 1)
//->fieldCondition('body', 'value', '%media/%', 'like')
->fieldCondition('field_legacy_id', 'value', 'NULL', '!=')
->addMetaData('account', user_load(1)); // Run the query as user 1.
$result = $query->execute();
$nids = array_keys($result['node']);
$nodes = entity_load('node', $nids);
$count1 = 0;
$total = count($nodes);
$timestart = time();
print "Deleting $total nodes\n";
print "Timeleft in minutes: ";
foreach ($nodes as $node) {
$count1++;
if($count1%20) print ".";
else {
$timeleftestimate = round( ((time()-$timestart)/$count1*($total-$count1)) / 60, 1);
print $timeleftestimate;
}
node_delete($node->nid);
}
print "\n\nDeleted ".$count1 ." / ". $total ." nodes\n";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment