On Drupal 7.58, I deleted a content type for which thousands of nodes existed. On this version of Drupal, deleting the content type is allowed even if nodes still exist (see Does deleting a content type also delete all nodes of that type?)

How should I proceed to clean my database and remove any nodes of that content type?

Would it be safe to do something along the lines of:

SELECT * from node WHERE type = "content_type_name"

and use all the node ids (nid) from that query to delete rows from node_counter, node_comment_statistics, node_access and node_revision? And then delete the rows from node?

  • If you think this isn't worth the trouble, please mention it to me. I suspect it won't do any harm to leave those nodes around. I am planning to upgrade to 7.90, and then 9.4 in the future if that changes anything.
    – leducvin
    Jul 6, 2022 at 17:03
  • What PHP version is used, for that site?
    – apaderno
    Jul 6, 2022 at 20:01
  • You will get all the NIDs using that SQL query, now for deleting the node you can use the Drush command for a safer side:- drush entity-delete node 1 2 3 4 Jul 7, 2022 at 5:45
  • @apaderno It is on php 5.5.9. Will be upgraded to D9 on php 8.1 eventually.
    – leducvin
    Jul 7, 2022 at 11:39
  • @ManishSharma Thank you, will look at this drush command
    – leducvin
    Jul 7, 2022 at 11:40

1 Answer 1


The easiest thing to do would probably be:

  1. Create the content type you deleted, the important part here is to give it same machine name as before, this will make the old nodes that exist in the database be picked up as this new node type.
  2. Clear cache (to ensure that everything gets reset)
  3. Delete the nodes, depending on your system, you might want to use a batch job, or the content list to simply delete them, by code you can:
$nids = db_query("SELECT nid form {node} where type = 'the-type';")
  ->fetchAllKeyed(0, 0);
if ($nids) {
  1. Delete the node type after clean up is done.

Your idea is basically what needs to be done, but you will be missing related data, like taxonomy_index storing all tags for a node and potential other modules storing data for nodes.

Please note, the idea is not battle tested, but I believe I have done something like this before once or twice in the past.

  • Thank you! Wish I had thought of that. It worked with no unintended consequences as far as I can tell.
    – leducvin
    Jul 12, 2022 at 11:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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