Worklog deletion and time spent adjustment

Hi all,
I’m trying to do a simple thing: I want t delete all the worklogs created in a set timeframe.
To do this, I’m trying to use this code

def projectKey = 'TEST'
def startDate = '2020/12/01'
def endDate = '2021/01/09'
queryText = "worklogDate >= '" +startDate+ "' AND worklogDate < '" + endDate + "' AND project = " + projectKey
//Executre the query
try{
query = jqlQueryParser.parseQuery(queryText)
}catch(Exception ex){
log.info("Error executing the query: " + queryText)
}

def search = searchService.search(user, query, PagerFilter.getUnlimitedFilter())

//Iterate the issues 
search.results.each { documentIssue ->
MutableIssue issue = issueManager.getIssueObject(documentIssue.id)

def workLogManager = ComponentAccessor.getWorklogManager()
def logsForIssue = workLogManager.getByIssue(issue)

//Iterate the worklog of the issue 
for(Worklog worklog : logsForIssue)
{
def date = new SimpleDateFormat("yyyy/MM/dd").format(worklog.getCreated())
if((date >= startDate) && (date < endDate)){
//Delete the worklog
workLogManager.delete(worklog.getAuthorObject(), worklog, null, true)
}
}
}

The above code works fine when there’s only one worklog to delete: the worklog is deleted and the time is correctly updated.

Things get different when an issue has 2 or more to be deleted. In this case I’ve found that:
the delete call deletes correctly all the worklogs
the time spent field is being updated taking into account the latest worklog only

This is a real trial I’ve made using a sleep function after the delete call.
Initial situation:
worklog 1: 1 hour
worklog 2: 3 hours
worklog 4: 8 hours
time spent: 12 hours (1+3+8)

With the above code, the result is:
worklog 1 is deleted -> time spent 11 hours
worklog 2 is deleted -> time spent 9 hours
worklog 3 is deleted -> time spent 4 hours

So, from what I saw the time spent field is being reset every time at the initial value

Hello @ClaudioGhisi

Do you maybe find some solution to overcome this?