script to delete all nodes of a type product after a csv import into ubercart

bowwowadmin - December 16, 2007 - 05:23

this script works to import products into ubercart from csv
http://www.ubercart.org/forum/development/803/csv_import_0_1_alpha

now I need a script to delete all the nodes of a certain type (products) because I now have about 5,000 duplicate products.

this is the script I tried but I get a white screen and it only deletes about 100 nodes at a time I have 15,000 to go.

<?php
  $result
= db_query("SELECT nid FROM {node} WHERE type LIKE 'product'");
  while (
$data = db_fetch_array($result)) {
   
node_delete($data['nid']);
  }
?>

I have bumped my settings up for this.
my php.ini
max_execution_time = 2500
memory_limit = 200M
upload_max_filesize = 20M
post_max_size = 40M

I would love some help on this its a little above my head as to it timing out.

oh by the way Drupal and Ubercart Rock...

Eric the man who used to have a life.

...

mooffie - December 16, 2007 - 11:35

If it timeouts on 100 items, let's delete only 30 and repeat this again and again and again (by refreshing the page). Sounds primitive, but actually that's how D6's Batch API works. Haven't tried this code, but it should work.

<?php
  $amt
= 30; // how many items to delete in one go?

 
$result = db_query_range("SELECT nid FROM {node} WHERE type LIKE 'product'", 0, $amt);

 
$n = 0;
  while (
$data = db_fetch_array($result)) {
   
node_delete($data['nid']);
    ++
$n;
  }

  if (
$n == $amt) { // is there more to delete?
   
drupal_goto($_GET['q']); // yep.
 
}
?>

Thanks for the reply will

bowwowadmin - December 16, 2007 - 18:20

Thanks for the reply will this work in 5.x ?

...

mooffie - December 17, 2007 - 00:07

Yes.

Note that it will refresh the page 500 times (15,000 items / 30 per turn). You can open another tab in your browser to inspect the progress.

For the record:

if you have shell access to your server then there's another way to do this. When PHP is run from the command line it is not restricted by 'max_execution_time'. Create a php file, in the same folder containing `index.php', and in it put:

<?php
$_SERVER
['HTTP_HOST'] = ''; // update this if your 'settings.php' isn't
                            // in the default place.
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

// Put your original code here...
?>

Then run this file (type php myprog.php at the prompt).

With this code it still

bowwowadmin - February 8, 2008 - 03:01

With this code it still times out but after LIKE 800 items or so..

Eric

...

mooffie - February 8, 2008 - 04:28

Then my assumption, that in CLI mode PHP ignores the time-limit setting, was incorrect. Sorry for misleading you and the readers.

Well. Let me venture with another unverified claim ;-) that doing set_time_limit(0) in a CLI script (CLI stands for 'command line interface') never fails.

set_time_limit?

gagarine - February 6, 2008 - 16:05

You can also use the set time limit but it's not allowed on all server.

A question: why you use "like" and note "where" in your query?

the version with set_time_limite:
<?php
$aquery = db_query("SELECT n.nid FROM {node} n WHERE n.type = 'product'");
if(db_num_rows($aquery) && user_access('administer nodes')) {
while ($n = db_fetch_object($aquery)) {
set_time_limit(5);
node_delete($n->nid);
}
echo 'ok, all product are deleted';
}
else {
echo "No product found or you do not have permission to modify nodes.";
}

...

mooffie - February 6, 2008 - 16:42

You can also use the set time limit

Right, that's what I suggest in half the places where I'm asked this same question :-)

but it's not allowed on all server.

Right, and even if it's allowed, the webserver itself may have a timeout of its own.

A question: why you use "like" and note "where" in your query?

That's because I copied this line as-is from the original poster's code. When I answer questions I try not to change irrelevant details in order not to confuse the asker. But you're correct, 'LIKE' isn't needed here.

double posting.. sorry

gagarine - February 6, 2008 - 17:13

double posting.. sorry

Also works for taxonomy terms

marcvangend - July 4, 2008 - 11:17

With a little adjustment, this scripts also works for taxonomy terms:

  $amt = 75; // how many items to delete in one go?

  $result = db_query_range("SELECT tid FROM {term_data} ", 0, $amt);

  $n = 0;
  while ($data = db_fetch_array($result)) {
    taxonomy_del_term($data['tid']);
    ++$n;
  }

  if ($n == $amt) { // is there more to delete?
    drupal_goto($_GET['q']); // yep.
  }

If you only want to delete the terms from a specific vocabulary, you could add a where-clause to the db query, for instance WHERE vid = 3.

Do anybody know how to

svihel - August 21, 2008 - 13:30

Do anybody know how to ensure that nodes with some taxonomy term wouldn't be deleted? I'm in similar situation like bowwowadmin, I also using ubercart and also importing nodes from time to time from different database. So i usually delete all products first and then upload new versions (mostly only with different price, but doesnt matter now, just for cleared picture..), but I also would like to have few products with parameter (taxonomy term) for example Permanent which wouldn't be affected by the deleting script.
Any ideas?

 
 

Drupal is a registered trademark of Dries Buytaert.