Ofttimes, when developing a site, you can be led to create or import a large number of custom nodes, for instance flexinodes, only to have to delete them later on. Doing it by hand in admin/content is inconvenient when the number of nodes is large, and for custom nodes like flexinodes, the exact entries to delete in the various Drupal tables may not be obvious. Time for a workaround.
The basic idea is to leverage the capabilites of the module defining the custom nodes:
it will hopefully always include an implementation of
hook_delete
,
which means Drupal will be able to delete these nodes cleanly
using node_delete
once the nid
of each node to be deleted is known.
Using this, it's just a matter of selecting the nodes,
typically based on their type, and invoking node_delete()
in a loop.
But there's a catch: on most shared hosting plans, the execution of a script
is host-limited and cannot be raised by the webmaster
(in other words set_time_limit()
is unavailable on these plans).
So any code will have to be executed several times. To optimize these recurring
executions, we can check the time elapsed while looping on the deletions and
stop a few seconds before the bell rings and the script is killed.
Here is a possible code example for this:
WARNING: this code runs with absolutely no protection when executed as described below and can totally ruin your site. Be sure you have a backup before using it and understand what you're doing with it.
Now this leaves a question opened: where should this code go ?
There is in fact a very simple, although not very visible, way to execute
arbitrary PHP code on a drupal server: this is the Drupal devel.module
,
usually not listed amongst the available modules for download on drupal.org,
because it is only intendend for use by developers, and not just basic sysadmins.
Once installed, it enables a menu choice leading to a page from which to run
a PHP piece of code, in which the code above can be pasted and tweaked to
one's needs.
good tips :)
Thx for sharing!
Thx for sharing!
Drupal version
Hi, just what I need :). Looking at article date this will not apply on current Drupal versions. Is this compatible with Drupal 6?
Thanks!
Just fine in D6
That's the whole point: since it is using the Drupal node API to delete the node, it should work with CCK nodes on D6 too, if that's what your considering.
For D7, you'll likely need to convert to the new DBTNG API instead of traditional
db_query()
, but the deletion mechanism is the same.Correction for Drupal 4.6
Nice addition, but ...
Nice addition, but ... are you really still using Drupal 4.6 ? This version has known issues and is no longer supported at all.