2

I'm trying to delete some content types which are no longer used but are still active.

I'm going to deploy the update using hook_update_N and have this so far (Code taken from node_type_delete_confirm_submit:

function hook_update_N(&$sandbox) {
  node_type_delete('mynodetype');
  variable_del('node_preview_mynodetype');
  node_types_rebuild();
  menu_rebuild();
}

The problem I'm having is that the node type is deleted, but the nodes of that type are still there in the database. Running cron hasn't deleted those nodes (I'm not even sure if that's the default behavior when deleting content types through the admin UI)

My question is:

  • Are the nodes deleted automatically after a certain time through Cron or some other method? If not then doing it manually, would it be best to do it before deleting the content type or after?

2 Answers 2

3

Are the nodes deleted automatically after a certain time through Cron or some other method?

They are not; IIRC, the type property for these nodes will be set to an empty string. No content will be deleted, though.

would it be best to do it before deleting the content type or after?

It probably doesn't matter, hugely, but conventional wisdom would be to sever the connection from the node to the content type before deleting the type (whether that's deleting the nodes or moving them to another content type).

That way you're not stepping on the implied (but of course not enforced) referential integrity between those data types, and you're decreasing the potential surface area for while-we're-updating errors.

2
  • Cheers. In regards to the nodes, I can still see the type field in the DB with the (deleted) content type. But that's not really important to me.
    – Beebee
    Commented Sep 8, 2015 at 12:37
  • Ah ok @Beebee, it's probably that non-existent node types are shown as a blank string on the content admin form; I think that's what I'm thinking of
    – Clive
    Commented Sep 8, 2015 at 12:45
4
$ournewtype = 'voucher';
$sql = 'SELECT nid FROM {node} n WHERE n.type = :type';
$result = db_query($sql, array(':type' => $ournewtype));
$nodeids = array();
foreach ($result as $row) {
  $nodeids[] = $row->nid;
}
node_delete_multiple($nodeids);
node_type_delete($ournewtype);
1
  • Thanks! But I wasn't really asking for code.
    – Beebee
    Commented Sep 8, 2015 at 12:37

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.