Skip to content

Instantly share code, notes, and snippets.

@rjpeter2
Last active December 21, 2015 19:49
Show Gist options
  • Save rjpeter2/6356839 to your computer and use it in GitHub Desktop.
Save rjpeter2/6356839 to your computer and use it in GitHub Desktop.
Programmatically delete fields and content types
<?php
// Array of content types.
$types = array('page', 'article');
foreach ($types as $type) {
// Gather all the example content that might have been created while this
// module was enabled. Simple selects still use db_query().
// api.drupal.org/api/function/db_query/7
$sql = 'SELECT nid FROM {node} n WHERE n.type = :type';
$result = db_query($sql, array(':type' => $type));
$nids = array();
foreach ($result as $row) {
$nids[] = $row->nid;
}
// Delete all the nodes at once
// api.drupal.org/api/function/node_delete_multiple/7
node_delete_multiple($nids);
// Loop over each of the fields defined by this module and delete
// all instances of the field, their data, and the field itself.
// api.drupal.org/api/function/field_delete_field/7
foreach (field_info_instances('node', $type) as $field_name => $field) {
field_delete_field($field_name);
}
// Delete our content type
// api.drupal.org/api/function/node_type_delete/7
node_type_delete($type);
// Purge all field information
// api.drupal.org/api/function/field_purge_batch/7
field_purge_batch(1000);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment