Jump to content

Deleting excess images


opalepatrick
 Share

Recommended Posts

I accidentally batch uploaded too many images. So now I have 3 or 4 identical images per product in this case. I wanted to delete all but the first.

I have tried umpteen options using the API but am a bit stumped. Could someone put me on the right track?

Thanks

Link to comment
Share on other sites

Thanks for the pointer adrian. I took so long because I tried to solve the issues I came across. Unsuccessfully!

The adapted code I used:

$p = $pages->get("parent=/products/");
$p->of(false);
$i=0;
foreach($p->prod_image as $image){
    //if($i!=0) $p->images->delete($image);
    if($i!=0){
    	echo $image . " selected to delete<br />";
    } 
    $i++;
}

It took me a while to realise that it was only getting the first product where the parent was products. So $p was a one page array? There should be at least 300+ items in the array. Am I getting confused. Anyway, going around in circles and in danger of disappearing up.... 

Not a pretty idea. Any help once again? :-)

Link to comment
Share on other sites

In a bit of a rush, but ->get will only ever return one page. You need ->find if you want to get all pages with parent=/products/

So maybe:

$all_pages = $pages->find("parent=/products/");
foreach($all_pages as $p){
    $p->of(false);
    $i=0;
    foreach($p->prod_image as $image){
      //if($i!=0) $p->images->delete($image);
      if($i!=0){
        echo $image . " selected to delete<br />";
      }
      $i++;
   }
}

Note - really rushed and untested, but hopefully you get the idea.

Link to comment
Share on other sites

Thanks for the feedback. It worked as above and I printed out precisely the correct files. However, when I then tried the delete line. It appeared to do something (no errors) but didnt actually delete anything. I then tried remove and the same thing. I have seen other threads that talks about using remove, but as I say nothing disappeared. Latest code:

$all_pages = $pages->find("parent=/products/");
foreach($all_pages as $p){
	$p->of(false);
	$i=0;
	foreach($p->prod_image as $image){
	    if($i!=0) $p->prod_image->remove($image);
	    // if($i!=0){
	    // 	echo $image . " selected to delete<br />";
	    // } 
	    $i++;
	}
}
$p->save("prod_image");

I am still looking, but a bit baffled.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...