Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tommy-thomas/1ce6f921a7700e33843c to your computer and use it in GitHub Desktop.
Save tommy-thomas/1ce6f921a7700e33843c to your computer and use it in GitHub Desktop.
Delete all nodes of a content type in Drupal 6
<?php
// See this thread for more info: http://drupal.org/node/92861
// Display our form
// Bumping up ex time to 5 minutes
ignore_user_abort(true);
ini_set('max_execution_time', 600);
set_time_limit(0);
print "<p>Note that it may take several minutes if you are deleting a lot of nodes.</p>";
//Useful hack for quick and dirty pseudo-module.
// combo of print and drupal_get_form; paste code into wysiwyg with php formatting.
print drupal_get_form('form_del_all_by_node_type');
// Our form for deleteing nodes of given type
function form_del_all_by_node_type()
{
$form['node_type'] = array(
'#type' => 'textfield',
'#title' => t('Content Type to delete:'),
'#description' => t('Enter the type of content to delete. This must be the exact content name used in the database.'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Delete now'),
);
$form['#attributes'] = array(
'enctype' => 'multipart/form-data',
'id' => 'form_del_all_by_node_type',
);
return $form;
}
// We do no validation
function form_del_all_by_node_type_validate($form_id, $form_values)
{
// left blank
}
function form_del_all_by_node_type_submit($form_id, $form_values)
{
$node_type = $form_values['values']['node_type'];
$total_node_count = isset($form_values['values']['total_node_count'])
? $form_values['values']['total_node_count']
: _get_total_node_count($node_type);
batch_delete_process($node_type , $total_node_count);
}
function batch_delete_process($node_type,$total_node_count)
{
// For this example, we decide that we can safely process
// 100 nodes at a time without a timeout.
$limit = 100;
$total = _get_total_node_count($node_type);
// With each pass through the callback, retrieve the next group of nids.
$query = "SELECT n.nid FROM newsmachine_node n WHERE n.type = '" . $node_type . "' limit " . $limit;
$aquery = db_query($query);
if( $total > 0 ){
while ($n = db_fetch_object($aquery)) {
// Here we actually perform our processing on the current node.
node_delete($n->nid);
}
if( $total > $limit ){
sleep(3);
$form_state['values']['node_type'] = $node_type;
$form_state['values']['total_node_count'] = $total_node_count;
$message = 'Processing...';
//$message .= theme('item_list', $results); // D6 syntax
drupal_set_message($message);
drupal_execute('form_del_all_by_node_type', $form_state);
} else {
// Here we do something meaningful with the results.
$message = $total_node_count.' deleted.';
//$message .= theme('item_list', $results); // D6 syntax
drupal_set_message($message);
}
} else {
drupal_set_message(t("There were no nodes of type ".$node_type." found."));
}
}
function _get_total_node_count($node_type = null)
{
$query = "SELECT count(*) as total FROM newsmachine_node WHERE type = '" . $node_type . "'";
return db_result(db_query($query));
}
@tommy-thomas
Copy link
Author

Copy and paste into Drupal wysiwyg with PHP as text format.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment