2

I would like to remove some items from a table using Impex. The following example throws no errors, but nothing is removed.

REMOVE ProductReference;pk[unique=true]
"#% impex.exportItemsFlexibleSearch(""select {pk} from {ProductReference as pr} where {pr.referenceType}=( {{select {pk} from {ProductReferenceTypeEnum as prte} where {prte.code} = 'CROSSELLING'}})"");"

The query produces results as expected. Is REMOVE not compatible with flexible search, or am I missing something?

The problem is, that I am running an import over hotfolder and I want to remove all existing items beforehand. Alternative solutions are welcome.

NaN
  • 7,441
  • 6
  • 32
  • 51

3 Answers3

4

Importing the query-

REMOVE ProductReference;pk[unique=true]
"#% impex.exportItemsFlexibleSearch(""select {pk} from {ProductReference as pr} where {pr.referenceType}=( {{select {pk} from {ProductReferenceTypeEnum as prte} where {prte.code} = 'CROSSELLING'}})"");"

is not working because you have not selected the Enable code execution checkbox.Enable code execution checkboxAlso, as suggested by @B.M replacing the script with impex.includeSQLData() and #% impex.initDatabase() would not have any effect if the checkbox is not selected.

However, selecting the checkbox and running the above script will give error, because there is no method by the name, exportItemsFlexibleSearch in the class MyImpExImportReader(which is called on running import).

The method exportItemsFlexibleSearch is available in DeprecatedExporter (which is called on running export not import).

Now, running this impex script in export will execute successfully without any error, but it won't remove anything. Instead, it will create a zip file with an impex and a script file. This script file will have the impex header for removing the items returned by the query. Using this zip file we can delete the items, conditionally.

Go to HMC -> Cronjobs -> Create a new cronjob of type Impex import job -> Upload the zip file in media attribute -> Create -> Run the impex.

This would delete the items returned by the query.

There is another way of deleting the items selected by the query.

Farrukh Chishti
  • 7,652
  • 10
  • 36
  • 60
  • 1
    What is that other way? I'm running into similar scenario – dwergkees Apr 09 '19 at 13:21
  • 1
    @dwergkees : good old groovy or beanshell script should do the trick – Farrukh Chishti Apr 09 '19 at 13:34
  • 1
    Thanks, this deserves its own question. With a groovy script i'm capable to delete a small number of products, but I get timeouts and lingering db locks with larger sizes. – dwergkees Apr 10 '19 at 09:56
  • @dwergkees : I have just created 10 million customers with groovy script and I did not get any error. I have assigned the same groovy script to multiple cronjobs and executed them simultaneously. – Farrukh Chishti Apr 10 '19 at 10:24
2

We need to export the script to generate zip file of import script and media file.Resultant zip file need to be imported with enablecodeexecution checked

Alternatively groovy script can be executed, an example:

import de.hybris.platform.servicelayer.search.FlexibleSearchQuery;
flexibleSearchService = spring.getBean("flexibleSearchService")
modelService = spring.getBean("modelService")
query = "select {pk} from {trigger}";
flexibleSearchService.search(query).result.each 
{
  modelService.remove(it)
}
MaartenDev
  • 5,631
  • 5
  • 21
  • 33
0

use HAC -> SQL Query console to delete using direct SQL command. Enable Commit code After running the update go to Monitoring tab and clear the cache