Delete content from the site

To delete nodes:


$type = ['invoice'];
$nids = db_select('node', 'n')
           ->fields('n', array('nid'))
           ->condition('type', $type, 'IN')
           ->execute()->fetchCol();
if (!empty($nids)) {
  dsm($nids);
  node_delete_multiple($nids);
  drupal_set_message(t('Deleted %count nodes.', array('%count' => count($nids))));
}

To detele custom entity type:


$entityType = 'invoice';
$results = entity_load($entityType);
$entity_ids = array_keys($results);
entity_delete_multiple($entityType, $entity_ids);

OR:

$id = 32;
$entityType = 'competence';
$query = new EntityFieldQuery();
$result = $query->entityCondition('entity_type', $entityType)
  // ->entityCondition('bundle', $entityType)
   ->execute();
 if (isset($result[$entityType])) {
   unset($result[$entityType][$id]);
   $entity_ids = array_keys($result[$entityType]);
   entity_delete_multiple($entityType, $entity_ids);
 }

To delete Field Collections:

$entityType = 'field_collection_item';
$fc_name = ['field_chat_messages'];
$ids = db_select($entityType, 'fc')
          ->fields('fc', array('item_id'))
          ->condition('field_name', $fc_name, 'IN')
          ->execute()->fetchCol();
entity_delete_multiple($entityType, $ids);
dsm($ids);

To delete users of some role, with exceptions:

$entityType = 'user';
$results = entity_load($entityType);
 $query = db_select('users_roles', 'ur')
   ->fields('ur', array('uid'));
 $query->condition('ur.rid', ID_ROLE);
 $query->condition('ur.uid', 223, '<>');
 $query->condition('ur.uid', 4480, '<>');
 $query->condition('ur.uid', 4473, '<>');
 $result = $query->execute();
 $uids = $result->fetchCol();
 dsm($uids);
 entity_delete_multiple($entityType, $uids); 

Delete taxonomy terms:


$vocabulary = taxonomy_vocabulary_machine_name_load('programmes');
foreach (taxonomy_get_tree($vocabulary->vid) as $term) {
 if ($term->tid != 50) {
   taxonomy_term_delete($term->tid);
 }
}