cancel
Showing results for 
Search instead for 
Did you mean: 

how to clean tasklog for Prod

SAPSupport
Employee
Employee
0 Kudos
200

Dear, 

how to clean up tasklog


------------------------------------------------------------------------------------------------------------------------------------------------
Learn more about the SAP Support user and program here.
View Entire Topic
SAPSupport
Employee
Employee
0 Kudos

SAP Hybris recommends to cleanup the TaskLogs table periodically.

The information contained in this table can be interesting for an audit trail. Make sure to backup your database and only cleanup this table if you are sure you will not need these log entries anymore.

A valid approach to cleanup the TaskLogs could be to implement the MaintenanceCleanupStrategy to remove logs for processes that are in the SUCCEEDED state and that where completed before a certain number of days.




Here is sample groovy script to clean up tasklog

 

import de.hybris.platform.core.Registry
import de.hybris.platform.core.model.task.TaskLogModel
import de.hybris.platform.servicelayer.search.FlexibleSearchQuery
import de.hybris.platform.servicelayer.search.SearchResult
import de.hybris.platform.servicelayer.search.impl.DefaultFlexibleSearchService
import de.hybris.platform.servicelayer.model.ModelService

// Get the FlexibleSearchService and ModelService
FlexibleSearchService flexibleSearchService = Registry.getApplicationContext().getBean("flexibleSearchService")
ModelService modelService = Registry.getApplicationContext().getBean("modelService")

// Specify the number of days to keep TaskLog entries
int daysToKeep = 30

// Calculate the cutoff date
Calendar calendar = Calendar.getInstance()
calendar.add(Calendar.DAY_OF_YEAR, -daysToKeep)
Date cutoffDate = calendar.getTime()

// Create a FlexibleSearch query to find TaskLog entries older than the cutoff date
String queryString = "SELECT {pk} FROM {TaskLog} WHERE {creationtime} < ?cutoffDate"
FlexibleSearchQuery query = new FlexibleSearchQuery(queryString)
query.addQueryParameter("cutoffDate", cutoffDate)

// Execute the query
SearchResult<TaskLogModel> result = flexibleSearchService.search(query)

// Delete the old TaskLog entries
result.getResult().each { taskLog ->
    try {
        modelService.remove(taskLog)
        println "Deleted TaskLog entry with PK: ${taskLog.pk}"
    } catch (Exception e) {
        println "Failed to delete TaskLog entry with PK: ${taskLog.pk} due to: ${e.message}"
    }
}

println "TaskLog cleanup complete. Deleted ${result.getResult().size()} entries older than ${daysToKeep} days."