I want to delete a node after it has been added (or modified), if certain conditions happen.

Is it possible to do that with hook_node_insert? (Considering hook_node_insert will actually be executed just before the node is added to the database.)

4 Answers 4


I think Rules module will be helpful for you in this kind of scenario. Rules module help us to define actions on different events(like user creation, node creation/updation/deletion). It is a very powerful module.

  • Initially I have used rules and it was working but the conditions has some nested inter-connections which does not seem it can be covered easily by rule (even by extending usage of rules' components). But with EntityFieldQuery I can cover all conditions pretty straight forward)! This is the main reason I am trying to solve the problem via a module! Commented Apr 21, 2015 at 14:19

With a bit of twist, I have managed to find a solution (it is not ideal but it works fine):

Instead of checking the conditions after inserting a new node, I have changed the algorithm to validate the node by using hook_node_validate to avoid saving the node if the conditions have not been met!

In that way I apply my conditions in another function (YOURMODULE_validate_pipeline()) by checking two reference values against some conditions. I use another variable to check if the conditions have been met (TRUE/FALSE) and set a suitable message accordingly! In my case the type of node was one of main factors to apply different conditions(you can use it in different scenario really)! Here is the code (hope it can help people with similar problem):

 * Implements hook_node_validate().
function YOURMODULE_node_validate($node, $form, &$form_state) {
  $YOUR_node_validated = array('validated' => TRUE,
                                 'message' => t('Node has been saved successfully'),);
  switch ($node->type) {
    case 'my_first_type':
      $YOUR_node_validated = YOURMODULE_validate_pipeline($node->field_one[$node->language][0]['target_id'],

      // code...
  if (!$YOUR_node_validated['validated']) {
    form_set_error($node->type, $YOUR_node_validated['message']);

You can use hook_view() since this hook is going to be called just after insert/update a node(If you have not changed default behavior after inserting a node). You can add a condition to check the user authority before execute your code.

  • Thanks, but that can be problematic and cause unnecessary overhead on the system; because hook_view will be used for all nodes when they are viewed! For example, when you view a normal node (not newly inserted node), the code will be executed! Not only this is a redundant operation but also it is against the algorithm (this checks and deletion supposes to be run just after the insertion or update)! Commented Apr 21, 2015 at 13:03
  • You can filter out any node any node type using conditions inside either this hook or hook_node_view(). Inside the hook function you can add your code right? There is no post insert/update hooks available.
    – ErangaJ
    Commented Apr 22, 2015 at 4:41
  • I have provided the solution, you can give it a try! In term of hook_view(), let me give you a simple explanation to see the redundancy: I have maximum 4 insert/update related to specific type but have about 1 million view on the same type! By using hook_node_view() my code will be executed about 1 million times! Considering these nodes should not be checked at all because they have not be updated at all! If you are app is a bit transactional-based, this will kill the app completely! This is the main reason we use hook_node_view() just when a node is viewed! Commented Apr 22, 2015 at 8:16

There seems to be a module that provides post-save hooks... Hook Post Action.

According to its description:

Currently Drupal core does not offer any hook to do actions after a node/entity is insered/updated/deleted in Database. So this module introduces several new Drupal hooks to overcome this limitation

  • hook_entity_postsave
  • hook_entity_postinsert
  • hook_entity_postupdate
  • hook_entity_postdelete
  • hook_node_postsave
  • hook_node_postinsert
  • hook_node_postupdate
  • hook_node_postdelete

(seen on StackOverflow: https://stackoverflow.com/a/24035797)

Hope it helps,

  • Thanks, the concept of the project seems very related but unfortunately still 'Recommended releases' are not there! I will look at their code later. Commented Apr 21, 2015 at 14:16
  • The module is extremely simple - you may find if you are writing your own that it is so easy to crib the relevant code from Hook Post Action that it is not worth installing the actual module. Commented Apr 21, 2015 at 16:14

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.