8 – Remove each node programmatically and automatically 2 years after its creation

My question has a half answer here but not finished for my case.

That's the piece of code I'm talking about:

getStorage (& # 39; node & # 39;);
$ query =  Drupal :: entityQuery (& # 39; node & # 39;)
-> accessCheck (FALSE)
-> condition (& # 39;) & # 39 ;, strtotime (& # 39; - 2 years & # 39;), & # 39;<=')
    ->range (0.50);
$ result = $ query-> execute ();
if (! empty ($ result)) {
$ nids = array_keys ($ result);
$ nodes = $ storage_handler-> loadMultiple ($ nests);
$ storage_handler-> delete ($ nodes);
}
}

On one of my projects, cron should run every 5 minutes.

I want when:

  1. Cron Run and
  2. at the end of the day it is midnight or between 23:45 and 00:00

remove by arbitrary programming only 50 knots from my site that meet the conditions below:

  1. Of type: article (if only) and
  2. the creation of the node is over 2 years old.

The code above will:

  1. Delete 50 nodes at a time. (WELL)
  2. Who have the time to create node more than 2 years. (WELL)
  3. Delete all nodes of any type (NOT OK)
  4. S & rsquo; will execute at every cron run (NOT GOOD)

So … how to modify the code above for:

  1. Run only on Cron and the end of the day is reached the time of day is therefore 00h00 (midnight) or between 23h45 and 00h00?

  2. Delete only nodes of type: article (if only) ?

If someone has a better idea on how to implement what I need, share it here!