Is there a way, in Drupal or in PHPMYADMIN, to delete all of a certain node type in the database? Specifically I need to periodically delete all Ecommerce products on the site and re-upload them. Thanks.

Comments

mooffie’s picture

You can use code:

$res = db_query("SELECT n.nid FROM {node} n WHERE n.type = 'product'");
while ($n = db_fetch_object($res)) {
  node_delete($n->nid);
}

Or you can combine the 'views' and the 'actions' modules to delete all the products.

Is there a way [...] in PHPMYADMIN

Don't try this. The data for a single node is strewn over several tables, and there's a lot of bookkeeping to do.

bookguy’s picture

Thanks for the reply, but I'm pretty new to php. Where and how do I execute the code you gave?

mooffie’s picture

How to execute PHP code?

Here are two different ways:

1. Create a new 'page' or 'story' or whatever, and in the Input Filter box choose 'PHP'. Now, whenever you view (or preview) this node, the PHP code it contains gets executed;

2. Install the 'devel' module. It gives you a textarea where you can type PHP code to execute. Paste there the code I gave you.

I believe the handbooks have a little intro on PHP code.

===

(I did a quick search but couldn't find a straighforward way to use 'views' with 'actions'. There's 'view_scheduler', but...)

(Just for the record: it's better to use db_query_range() instead of db_query() if we have lots of products, or a slow server, to prevent the server timeouting on us in the middle of a node delete. But I want to believe you can ignore this little comment; it's for completeness sake.)

bookguy’s picture

the code seems to work with db_query(), though db_query_range() doesn't seem to do anything to the products. The page with the code just loads normally and nothing else is affected. I do have quite a few (several thousand) products, so I'm running the code repeatedly. It would be nice if there were something with just a single step, but without better PHP skills I guess I'll take what I can get. Thanks again.

mooffie’s picture

I'm running the code repeatedly

You mean, the server timeouts the script, correct? After how many seconds? (and how many nodes does it manage to delete?)

Try adding the following line at the beginning:

set_time_limit(240);
bilevader’s picture

I had the exact same problem (and skill level) as bookguy and this solution worked perfectly. I took approach #1. Indeed it was timing out for me before adding the last snippet. Thanks.