'Delete all content', 'description' => 'Delete all nodes and comments on this site. This is useful for development sites, or prior to launching the site. Otherwise it destroys all data on a site.', 'page callback' => 'drupal_get_form', 'page arguments' => array('delete_all_content'), 'access arguments' => array('administer nodes'), 'type' => MENU_NORMAL_ITEM); $items['admin/content/delete_users'] = array( 'title' => 'Delete all users', 'description' => 'Delete all users on this site. This is useful for development sites, or prior to launching the site. Otherwise it destroys all data on a site.', 'page callback' => 'drupal_get_form', 'page arguments' => array('delete_all_users'), 'access arguments' => array('administer users'), 'type' => MENU_NORMAL_ITEM); return $items; } function delete_all_content() { drupal_add_js(drupal_get_path('module', 'delete_all') .'/delete_all.js'); $form = array(); $form['all'] = array( '#type' => 'checkbox', '#default_value' => TRUE, '#title' => t('Delete All Content'), '#description' => t('Select to delete all content'), '#attributes' => array('class' => 'delete-all'), ); // count how many nodes we have of each type $result = db_query("SELECT type, COUNT(*) AS num FROM {node} GROUP BY type"); $count = array(); while ($data = db_fetch_object($result)) { $count[$data->type] = $data->num; } // add the types to the form $types = array(); foreach (node_get_types() as $type => $info) { if ($count[$type] > 0) { $types[$type] = $info->name .' ('. $count[$type] .')'; } } asort($types); $form['type-fieldset'] = array( '#type' => 'fieldset', '#title' => t('Types'), '#collapsible' => TRUE, '#collapsed' => TRUE, 'types' => array( '#type' => 'checkboxes', '#options' => $types, '#description' => t('Select the types of content to delete'), '#theme' => 'delete_all_checkboxes', ), ); $form['method-fieldset'] = array( '#type' => 'fieldset', '#title' => t('Method'), '#collapsible' => TRUE, '#collapsed' => TRUE, 'method' => array( '#type' => 'radios', '#title' => t('Method'), '#options' => array('normal' => t('Normal'), 'quick' => t('Quick')), '#default_value' => 'normal', '#description' => t('Normal node delete calls node_delete() on every node in the database. If you have only a few hundred nodes, this can take a very long time. Use the quick node delete method to get around this problem. This method deletes directly from the database, skipping the extra php processing. The downside is that it can miss related tables that are normally handled by module hook_delete\'s.'), ), ); $form['confirm'] = array( '#type' => 'checkbox', '#title' => t('Delete Confirmation'), '#description' => t('Please check this box to confirm that you understand you are deleting node content. This should only be done in a development environment.'), '#required' => TRUE, ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Delete'), ); $form['#submit'] = array('delete_all_content_submit'); return $form; } /** * Implementation of hook_theme for Drupal 6 theme registry */ function delete_all_theme() { return array( 'theme_delete_all_checkboxes' => array( 'arguments' => array('form' => NULL), ), ); } function theme_delete_all_checkboxes($form) { $total = 0; foreach ($form as $element_id => $element) { if ($element_id[0] != '#') { $total ++; } } $total = (int) (($total % 3) ? (($total + 2) / 3) : ($total / 3)); $pos = 0; $rows = array(); foreach ($form as $element_id => $element) { if ($element_id[0] != '#') { $pos ++; $row = $pos % $total; $col = $pos / $total; if (!isset($rows[$row])) { $rows[$row] = array(); } $rows[$row][$col] = drupal_render($element); } } return theme('table', array(), $rows); } function delete_all_content_submit($form, &$form_state) { $types = array(); if (!$form_state['values']['all']) { foreach ($form_state['values']['types'] as $type => $checked) { if ($checked) { $types[] = $type; } } } switch ($form_state['values']['method']) { case 'normal': $count = _delete_all_normal($form_state['values']['all'], $types); break; case 'quick': // the quick method doesn't support an all option, // so just get all the content types and delete all of those if ($form_state['values']['all']) { $result = db_query("SELECT DISTINCT type FROM {node}"); while ($data = db_fetch_object($result)) { $types[] = $type; } } $count = _delete_all_quick($types); break; } if ($form_state['values']['all']) { // Delete the URL aliases db_query("DELETE FROM {url_alias} WHERE src LIKE 'node/%%'"); drupal_set_message(t('All nodes, comments and URL aliases have been deleted. Number of nodes deleted: !count.', array('!count' => $count))); } else { drupal_set_message(t('Nodes and comments of type @type have been deleted. Number of nodes deleted: !count.', array('!count' => $count, '@type' => implode(', ', $types)))); } drupal_goto('admin'); } function _delete_all_normal($all, $types) { if ($all) { $result = db_query('SELECT nid FROM {node}'. $where); } else { $placeholders = implode(',', array_fill(0, count($types), "'%s'")); $result = db_query('SELECT nid FROM {node} WHERE type IN ('. $placeholders .')', $types); } $deleted = 0; while ($data = db_fetch_object($result)) { set_time_limit(30); node_delete($data->nid); $deleted ++; } return $deleted; } function _delete_all_quick($types) { $deleted = 0; foreach ($types as $type) { // keep this alive set_time_limit(240); // determine how many items will be deleted $count = db_result(db_query("SELECT COUNT(*) FROM {node} WHERE type = '%s'", $type)); if ($count) { // should always be positive /** * build a list of tables that need to be deleted from * * The tables array is of the format table_name => array('col1', 'col2', ...) * where "col1, col2" are using "nid, vid", but could simply be "nid". */ $nid_vid = array('nid', 'vid'); $nid = array('nid'); $tables = array('node_revisions' => $nid_vid, 'comments' => $nid); $tables[_content_tablename($type, CONTENT_DB_STORAGE_PER_CONTENT_TYPE)] = $nid_vid; $content = content_types($type); if (count($content['fields'])) { foreach ($content['fields'] as $field) { $field_info = content_database_info($field); $tables[$field_info['table']] = $nid_vid; } } // find all other tables that might be related switch ($GLOBALS['db_type']) { case 'mysql': case 'mysqli': $result_tables = db_query("SHOW TABLES"); while ($data = db_fetch_array($result_tables)) { $table = array_pop($data); if (isset($tables[$table]) || substr($table, 0, 8) == 'content_') { continue; } $result_cols = db_query("SHOW COLUMNS FROM %s", $table); $cols = array(); while ($data = db_fetch_array($result_cols)) { $cols[$data['Field']] = $data; } if (isset($cols['nid'])) { $tables[$table] = isset($cols['vid']) ? $nid_vid : $nid; } } break; case 'pgsql': // @TODO: inspect the database and look for nid fields break; } // @todo: update all node related nid references // delete from all of the content tables in one sql statement $sql = array('delete' => array(), 'from' => array(), 'where' => array()); $index = 0; foreach ($tables as $table => $cols) { $table = '{'. $table .'}'; $sql['cols'][] = "t$index.*"; // build the ON clause $on = array(); foreach ($cols as $col) { $on[] = "t$index.$col = n.$col"; } // now that we have the ON clause, build the join clause $sql['join'][] = " LEFT JOIN $table t$index ON ". implode(' AND ', $on); $index ++; } $delete_sql = "DELETE n.*, ". implode(', ', $sql['cols']) ." FROM {node} n ". implode(' ', $sql['join']); db_query($delete_sql ." WHERE n.type = '%s'", $type); $deleted += $count; } } return $deleted; } function delete_all_users() { return confirm_form( $form, t('Are you sure you want to delete all users?'), 'admin', '', t('Delete'), t('Cancel'), 'delete_all_content'); } function delete_all_users_submit($form, &$form_state) { $result = db_query('SELECT uid FROM {users} WHERE uid > 1'); $count = 0; while ($data = db_fetch_object($result)) { user_delete(array(), $data->uid); $count++; } // Delete the URL aliases db_query("DELETE FROM {url_alias} WHERE src LIKE 'user/%%'"); db_query("UPDATE {sequences} SET id = 1 WHERE name = 'users_uid'"); drupal_set_message(t('All users have been deleted. Number of users deleted: !count.', array('!count' => $count))); drupal_goto('admin'); }