Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Safely Deleting Change History Records

Daniel Garcia
Contributor
December 1, 2019

I have a number of derived fields which create noise in the issue change log. I'd like to remove these from the log.

I've had a read of https://community.atlassian.com/t5/Jira-questions/Is-it-possible-to-delete-History-records-of-an-issue-like-the/qaq-p/254441 and take note of the warnings. I've also had a look at the code for com.atlassian.jira.issue.changehistory.removeAllChangeItems(Issue issue) in order to understand what it is doing and adapt it to my needs.

I'm wondering if the following code is the correct/safe way to delete change items, without needing to shutdown and restart the service.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.changehistory.ChangeHistoryManager
import com.atlassian.jira.issue.changehistory.ChangeHistory
import com.atlassian.jira.entity.property.JsonEntityPropertyManager
import com.atlassian.jira.entity.Entity
import com.atlassian.jira.entity.property.EntityPropertyType
import com.atlassian.jira.ofbiz.OfBizDelegator

List<String> fieldsToRemove = ["Field Name 1", "Field Name 2", "Field Name 3"]

ChangeHistoryManager changeHistoryManager =
ComponentAccessor.getChangeHistoryManager()
JsonEntityPropertyManager jsonEntityPropertyManager =
ComponentAccessor.getComponent(JsonEntityPropertyManager)
OfBizDelegator ofBizDelegator = ComponentAccessor.getOfBizDelegator()

List<ChangeHistory> history = changeHistoryManager.getChangeHistories(issue)
history.each{ChangeHistory ch ->
Boolean allDeleted = null
ch.changeItems.each {
Long changeItemId = it.get("id")
String field = it.get("field")
if (fieldsToRemove.contains(field)) {
if (allDeleted==null) {
allDeleted = true
}
ofBizDelegator.removeByAnd(Entity.Name.CHANGE_ITEM,
[id: changeItemId].asImmutable())
} else {
allDeleted = false
}
}

if (allDeleted) {
ofBizDelegator.removeByAnd(Entity.Name.CHANGE_GROUP,
[id: ch.id].asImmutable())
jsonEntityPropertyManager.deleteByEntity(
EntityPropertyType.CHANGE_HISTORY_PROPERTY.getDbEntityName(),
ch.id)
}
}

 

1 answer

1 vote
Marcus Watkins
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
January 18, 2022

Did this end up working out for you? We're considering something similar and testing of your approach seems fine. I'd love to know if you ran into any issues, though.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events