2

I want to delete the content of a custom content type, which has thousand of nodes. The problem is that I can delete the references from the node database table, but the issue in this solution is that the content still remains in the custom field types database table, and when I want to recreate some data, it gives me the error of duplicate data.

Is there a solution to delete custom content type nodes with there related custom field types?

I am using Drupal 7.

  • So you've deleted your nodes by deleting the entries in the node table? – Chapabu Nov 27 '12 at 14:06
  • Not all of them, Almost half.. But when i try to recreate them with old content with some tweaks, it says duplicate content, and after looking in the database, the content is still there.. – S. A. Malik Nov 27 '12 at 14:09
  • Does "delete the references from the node Database table" mean that you ran manual SQL queries to do the deletion and didn't use the Drupal API? – mpdonadio Nov 27 '12 at 14:13
0

You could use node_delete or node_delete_multiple to do this:

// Retrieve all nids for your custom content type.
$result = db_query("SELECT nid FROM {node} WHERE type = :node_type", array(":node_type" => "<YOUR_CONTENT_TYPE>"))
  ->fetchAll();

// Delete all nodes.
foreach($result as $nid) {
  node_delete($nid);
}

// You should also be able to use node_delete_multiple().
// node_delete_multiple($result);
  • Thanks! Helped but there was an error. I am posting the right code.. – S. A. Malik Dec 9 '12 at 7:38
  • @backTangent - Perhaps you could have asked me to correct the problem instead of making your own answer? I never tested the above code, but I though it would point you in the right direction. – Cyclonecode Dec 9 '12 at 10:48
  • I only did so that some one coming later should have the right answer, i tried to up vote you but my reputation didnot allowed me. – S. A. Malik Dec 15 '12 at 18:40
0

Below is the code which can be posted in any file and then executed. It will remove all the nodes of the specific content type.

// Retrieve all nids for your custom content type.
$result = db_query("SELECT nid FROM {node} WHERE type = :node_type", array(":node_type" => "<YOUR_CONTENT_TYPE>"))->fetchAll();

// Delete all nodes.
 foreach($result as $node) {
  node_delete($node->nid);
 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.