Skip to content

Instantly share code, notes, and snippets.

@zachseifts
Created November 30, 2010 16:35
Show Gist options
  • Save zachseifts/721933 to your computer and use it in GitHub Desktop.
Save zachseifts/721933 to your computer and use it in GitHub Desktop.
Deleting all nodes of a specific type in Drupal
<?php
/**
* Mass delete nodes of $nodetype
* put the file in the Drupal root and adjust $nodetype and $limit as you like
*
* Mostly borrowed from: http://stream.zerolab.org/post/917260717/mass-delete-nodes-in-drupal
* and http://www.stonemind.net/blog/2009/01/20/headless-drupal-using-drupals-api-to-batch-script-your-drupal-site/
*
*/
error_reporting(E_ALL);
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
$nodetype = "feed_ical_item";
$results = db_query("SELECT nid FROM {node} WHERE type='%s'", $nodetype);
$count = 0;
while ($result = db_fetch_object($results)) {
db_query('DELETE FROM {node} WHERE nid = %d', $result->nid);
db_query('DELETE FROM {node_revisions} WHERE nid = %d', $result->nid);
// Call the node-specific callback (if any):
node_invoke($result, 'delete');
node_invoke_nodeapi($result, 'delete');
// Clear the page and block caches.
cache_clear_all();
// Remove this node from the search index if needed.
if (function_exists('search_wipe')) {
search_wipe($result->nid, 'node');
}
watchdog('content', '@type: deleted %title.', array('@type' => $result->type, '%title' => $result->title));
drupal_set_message(t('@type %title has been deleted.', array('@type' => node_get_types('name', $result), '%title' => $result->title)));
print 'Deleted node: ' . $result->nid . '<br />';
$count++;
}
print "Total deleted: $count";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment