cancel
Showing results for 
Search instead for 
Did you mean: 

Impex Script to remove data based on date

Former Member
0 Kudos

Hi,

I was trying to generate an impex script that would allow me to delete all Carts that were created before 2015.

I am not able to understand how I can use operator for this

Thanks, Sooraj

Accepted Solutions (0)

Answers (4)

Answers (4)

Former Member
0 Kudos

Hi Sooraj, If you want to delete huge amount of cart data ,use sql query and run through hac or you you can run on database directly then clear cache from hybris hac.

Thanks and Regards Raushan

Former Member
0 Kudos

Hi Sooraj,

You can try the following script

 #% impex.enableCodeExecution(true)
 #% impex.enableExternalSyntaxParsing(true)
 #% impex.enableExternalCodeExecution(true)
 
 
 "#%
 
 import de.hybris.platform.core.Registry;
 import de.hybris.platform.servicelayer.model.ModelService;
 import de.hybris.platform.servicelayer.search.FlexibleSearchService;
 import de.hybris.platform.servicelayer.search.SearchResult;
 import de.hybris.platform.core.model.order.CartModel;
 
 
 FlexibleSearchService flexibleSearchService = (FlexibleSearchService) Registry.getApplicationContext().getBean(""flexibleSearchService"");
 ModelService modelService = (ModelService) Registry.getApplicationContext().getBean(""modelService"");
 
 SearchResult result = flexibleSearchService.search(""select {pk} from {Cart} WHERE {date} < '2015/10/20'"");
 
 for (Object object : result.getResult()) {
     if (object instanceof CartModel){
       CartModel cart = (CartModel) object;
       impex.warn(""Removing Cart = "" + cart.toString() + "" with date = "" + cart.getDate());
       modelService.remove(cart);
    }
 }
 
 "

where the date is specified in the flexible search query.

andyfletcher
Active Contributor
0 Kudos

Nice use of "impex" 🙂

Wouldn't you just run this as beanshell from the administration console?

Better yet use Groovy...

 flexibleSearchService.search("select {pk} from {cart} where {date} < '2015/10/20'").result.each {
     println "Removing cart ${it.code} with date ${it.date}"
     modelService.remove(it)
 }
Former Member
0 Kudos

Thanks Adrew and Vova for your inputs.

In our case as we have huge amount of data, bean shell or remove through Java(flexible search) was a bit risky as it might cause slowness to the end user.

We decided to follows the following steps. 1. Importing carts codes in production 2. Removing the carts with a impex file. Handling smaller chunks of data each time and executing them during off hours.

Regards, Sooraj

Former Member
0 Kudos

Hi Reena,

Thanks for your reply. We are not using hybris 5. If I am correct, I could not find this CronJob. However we have created a new Cronjob that would be deleting the records in a timely manner.

Because we found huge data in the cart we thought of having a one time impex script to clear the data.

Thanks, Sooraj

Former Member
0 Kudos

I meant clean up of the old cart before this new custom CronJob goes live

Former Member
0 Kudos

Hi Sooraj,

Is there any specific reason for not using oldCartRemovalCronJob?

There is cartRemovalAge attribute defining Carts older than a specified number of seconds will be removed.

Example for adding thesitesattribute with help of the ImpEx script file. #Setting Old Cart Cleanup CronJobs for wsTest site INSERT_UPDATE OldCartRemovalCronJob;code[unique=true];job(code);sites(uid) ;oldCartRemovalCronJob;oldCartRemovalJob;wsTest

Please have a look into following wiki document for detail information on How to remove old carts:

https://wiki.hybris.com/display/release5/Removing+Old+Carts+with+Cronjob

If you delete it through impex,cache will get out of sync since the cache won't have been notified about the deletes.

Regards,

Reena

Former Member
0 Kudos

Again, delete through impex is not flexible mechanism because each time you will need create query or impex file. If this mechanism should be run e.g each night, week month, good approach is to create new cronJob and scheduled it. It will run several time without writing impex file or query.