Logged time not decreasing after deleting worklog

Marketa Dvorackova May 15, 2015

Hi everyone,
I have imported worklogs in JIRA (JSON importer) on different issues and there has been a mistake which requires us to delete all of them.
I have prepared a script to do that but when the worklogs get deleted, the time spent (Logged - on Time tracking panel) doesn't decrease.
I've done some tests and this only happens for the worklogs imported in JIRA. Worklogs created manually and deleted with the same script don't cause any problems. The imported worklogs deleted manually work ok too.
What is surprising is that when I run the delete worklog, I get in the activity list exactly what I should, including "Removed the Worklog Time Spent".
Just for the completeness of the information, for the delete, I am using the JIRA API Interface WorklogManager

com.atlassian.jira.issue.worklog:
boolean delete(com.atlassian.crowd.embedded.api.User user,
             Worklog worklog,
             Long newEstimate,
             boolean dispatchEvent)

It's not that I have a problem with the script, it IS working. Deletes the worklogs. Is there more that needs to be done?

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.worklog.WorklogManager;
import com.atlassian.jira.issue.worklog.WorklogImpl;
import com.atlassian.jira.issue.worklog.Worklog;
import com.atlassian.crowd.embedded.api.User;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;

WorklogManager worklogManager =  ComponentAccessor.getWorklogManager();
Issue issue = ComponentAccessor.getIssueManager().getIssueObject("EPIC-3101");
Long newEstimate;
//take the issue and for each worklog, delete it (complete clean-up)
worklogManager.getByIssue(issue).each { WorklogImpl worklog ->
    //to get the remaining there was before this worklog
	newEstimate = issue.getEstimate()+worklog.getTimeSpent()
    worklogManager.delete(ComponentAccessor.getJiraAuthenticationContext().getUser().getDirectoryUser(), worklog, newEstimate, false)
    
}

The activity is interesting, because for the same one script run it is one of these three (not same):

image2015-5-15 14:46:24.png

out of which only the last one seems correct. I logged something? Seriously, by deleting a worklog?

If you have any idea what might be happening here, please let me know. Thanks a lot.

1 answer

0 votes
Jobin Kuruvilla [Adaptavist]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 15, 2015

WorklogManager gives you an option to adjust the remaining estimate and time spent when the worklog is deleted. And you are updating those fields in your code.

If you want JIRA to auto adjust those values when the worklog is deleted, use WorklogService. See deleteAndAutoAdjustRemainingEstimate method.

Marketa Dvorackova May 15, 2015

Hi Jobin, thank you for the suggestion, I will immediately have a look at that. I am adjusting only the remaining estimate though, not touching the time spent, only reading it on the worklog itself. I thought that the logged time has nothing to do with estimates and that it will get auto-adjusted whatever you are using to delete a worklog. It gets deleted, so detract.

Jobin Kuruvilla [Adaptavist]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 15, 2015

It is only adjusting the remaining estimate in the screenshot (to 4 hours. i.e 3+1). Timespent is reduced by the deleted worklog (3 hours). Anyways, WorklogService should do it for you.

Marketa Dvorackova May 15, 2015

Well yes, that is actually the point : "Timespent is reduced by the deleted worklog (3 hours)". It says so, but it doesn't DO that. I delete all of the worklogs and the Logged time remains the same. So I have 5 weeks logged on the issue with no worklog in the end. I will try the service though.

Mack Carr February 12, 2019

I am getting the same problem

Markus Fischbacher May 21, 2021

Hi there,

I'm facing the same issue.
If I delete the worklog (), then it's delete from the issue itself.

private static void deleteWorklog(final long worklogId) {
WorklogService worklogService = ComponentAccessor.getComponent(WorklogService.class);
JiraServiceContext jiraServiceContext = new JiraServiceContextImpl(ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser());
WorklogResult worklogResult = worklogService.validateDelete(jiraServiceContext, worklogId);
worklogService.deleteAndAutoAdjustRemainingEstimate(jiraServiceContext, worklogResult, false);
}


The UI is reflecting that by not showing any logged time.
But if I then log a new time, the old value is added to the newly written.

public static void createWorkLog(final Issue issue, final Date date, final String comment, final double amount) {
WorklogService worklogService = ComponentAccessor.getComponent(WorklogService.class);
JiraServiceContext jiraServiceContext = new JiraServiceContextImpl(ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser());
int min = (int) Math.round(amount * 60);
final WorklogInputParametersImpl.Builder builder = WorklogInputParametersImpl.issue(issue).timeSpent(min + "m")
.startDate(date).comment(comment).visibility(Visibilities.publicVisibility());
WorklogResult result = worklogService.validateCreate(jiraServiceContext, builder.build());
worklogService.createAndAutoAdjustRemainingEstimate(jiraServiceContext, result, false);
}

E.g: there have been 2 hours logged. After deleting the log entry using it's ID, the 2 hours are delete. After creating a new log entry with 1 hour, the logged time shows 3 hours. I've no idea why that happens.

Any ideas?

Markus

Suggest an answer

Log in or Sign up to answer