Developer forum

Forum » Development » Delete product API

Delete product API

Keld Gøtterup
Reply

When using the code below it doesn't actually delete anything. i do get the "hits" i want which i can see in my logs but it stille doesn't delete. What am i doing wrong?

public static void deleteMissingProducts()
        {
            logEntry("Start deleteMissingProducts");
            foreach (var prod in Dynamicweb.eCommerce.Products.Product.GetAllProducts())
            { 
                if(prodList.Where(x => x.ProductID == prod.Number).Count() == 0){
                    logEntry("DELETE:" + prod.ID + "-" + prod.VariantID + "-"+ prod.Number + " " + prod.Name);
                    prod.Delete();
                    
                    
                }
            }
            logEntry("End deleteMissingProducts");
        }


Replies

 
Nicolai Høeg Pedersen
Reply

Hi Keld

Yes, that is not very clear. instance.Delete is inherited from its base implementation but does not work for products.

So you need to call the static overload that takes a product id. Product.Delete(ProductID).

I take it that you are aware of the performance and memory issue of calling GetAllProducts() - of course depending on how many products you have in the solution. If it is 1mio it is a problem :-).

Happy coding, Nicolai

 
Keld Gøtterup
Reply

I get a message that this is deprecated, i also cant get much intellisense on this method and i am wondering what if i need to delete a variant?

I am well aware of the performance issue in this :)

 
Nicolai Høeg Pedersen
Reply

Hi Keld

Yes, my answer is wrong I can see. Product.Delete() does work - it deletes the language specific variant of the product. You loop Prod in all products - your LINQ uses prodList which is coming from where? That seems to be the wrong one - you log in Prod, but delete on prodList?

So what do you want to do? Products have a key of 3 elements - productid, variantid and languageid. Maybe you delete the swedish version of the product and you cannot see that...

So question is - what are you trying to delete? Entire product with all variants in all languages? All variants in one or all languages? Or something else?

 
Keld Gøtterup
Reply

the linq is for comparing and finding out which product i need to delete.

the idea is to delete products and/or specific variants, and there is only one language.

my logentry gives ie. "DELETE:9802425--9802425 Royal Canin Mini Adult 8 kg." which is apparently the correct product but it never deletes it no matter how often this code is run.

 
Nicolai Høeg Pedersen
Reply

Hi Keld

That did not answer my question :-).

What are you trying to delete? Because if you try to delete i.e. the danish product and runníng in the english language context, it will not be deleted - or you ony delete the one in the english context everytime, the product is still there in other language contexts.

 
Keld Gøtterup
Reply

There is only one language.

ive simplified the code to better illustrate the problem.

Im trying to delete specific products i find in GetAllProducts

public static void deleteMissingProducts()
        {
            logEntry("Start deleteMissingProducts");
            foreach (var prod in Dynamicweb.eCommerce.Products.Product.GetAllProducts().Take(1))
            { 
                    logEntry("DELETE:" + prod.ID + "-" + prod.VariantID + "-"+ prod.Number + " " + prod.Name);
                    prod.Delete();
            }
            logEntry("End deleteMissingProducts");
        }

 

 

Result when running this multiple times:

06-10-2015 11:03:10: Start deleteMissingProducts
06-10-2015 11:03:11: DELETE:115--115 Duroc PRRS Produktionssæd
06-10-2015 11:03:11: End deleteMissingProducts
06-10-2015 11:03:13: Start deleteMissingProducts
06-10-2015 11:03:13: DELETE:115--115 Duroc PRRS Produktionssæd
06-10-2015 11:03:13: End deleteMissingProducts
06-10-2015 11:03:15: Start deleteMissingProducts
06-10-2015 11:03:15: DELETE:115--115 Duroc PRRS Produktionssæd
06-10-2015 11:03:15: End deleteMissingProducts
06-10-2015 11:03:16: Start deleteMissingProducts
06-10-2015 11:03:17: DELETE:115--115 Duroc PRRS Produktionssæd
06-10-2015 11:03:17: End deleteMissingProducts
06-10-2015 11:03:18: Start deleteMissingProducts
06-10-2015 11:03:18: DELETE:115--115 Duroc PRRS Produktionssæd
06-10-2015 11:03:18: End deleteMissingProducts
06-10-2015 11:03:20: Start deleteMissingProducts
06-10-2015 11:03:21: DELETE:115--115 Duroc PRRS Produktionssæd
06-10-2015 11:03:21: End deleteMissingProducts
06-10-2015 11:03:21: Start deleteMissingProducts
06-10-2015 11:03:22: DELETE:115--115 Duroc PRRS Produktionssæd
06-10-2015 11:03:22: End deleteMissingProducts
06-10-2015 11:03:23: Start deleteMissingProducts
06-10-2015 11:03:23: DELETE:115--115 Duroc PRRS Produktionssæd
06-10-2015 11:03:23: End deleteMissingProducts

 
Nicolai Høeg Pedersen
Reply

That did still not answer my question...

Question is - what are you trying to delete? Entire product with all variants in all languages? All variants in one or all languages? Or something else?

BR Nicolai

 
Keld Gøtterup
Reply

i want to be able to delete specific products with and without variants. I also want to be able to delete specific variants without deleting the mainproduct. There is only 1 language.
 

 
Nicolai Høeg Pedersen
Reply

And from where do you execute your code? Backend, schedule, frontend, somewhere else?

 
Keld Gøtterup
Reply

this is gonna be a scheduled task

 
Keld Gøtterup
Reply

Scheduled task

 
Nicolai Høeg Pedersen
Reply
This post has been marked as an answer

Hi Keld

Products has a composite key of 3 fields - product id, language id and variant id. Calling GetAllProducts would give you a potentially huge collection of products in a specific language context. But it does not take i.e. variant id into consideration.

So when you call product.delete, it might not delete because you cannot delete the master if it has variants, or it is default language etc. etc. You would need to keep track of context.

So instead, create an empty propuct collection, and add the products to that one and call this method:

Product.Delete(ProductCollection products);

​That will handle that all language and variants of the products (based on their product id) are deleted.

BR Nicolai

Votes for this answer: 1
 
Keld Gøtterup
Reply

This is still listed as a deprecated function but it is working

 

 

 

 
Nicolai Høeg Pedersen
Reply

Hi Keld

It is not deprecated and it is one of the method that DW uses to delete products. It has an overload that is obsolete. You can also try DeleteAll method. 

 

 

 

You must be logged in to post in the forum