How do I delete a field configuration scheme via scriptrunner?

KD November 2, 2022

Quite simple what I'm trying to do, I have an array of field configuration schemes (projectlessFieldConfigSchemes) that I would like to delete. I attempt the code below to remove just the first of the array.

 

def fieldConfigSchemeMgr = ComponentAccessor.getFieldConfigSchemeManager()
def temp= projectlessFieldConfigSchemes[0] as FieldConfigurationScheme
fieldConfigSchemeMgr.removeFieldConfigScheme(temp.id)
The error I am getting is such:
java.lang.NullPointerException at com.atlassian.jira.issue.context.persistence.FieldConfigContextPersisterWorker.removeContextsForConfigScheme(FieldConfigContextPersisterWorker.java:90) at com.atlassian.jira.issue.context.persistence.CachingFieldConfigContextPersister.removeContextsForConfigScheme(CachingFieldConfigContextPersister.java:127) at com.atlassian.jira.issue.fields.config.manager.FieldConfigSchemeManagerImpl.removeFieldConfigScheme(FieldConfigSchemeManagerImpl.java:236) at DeleteInactiveFieldConfigSchemes.run(DeleteInactiveFieldConfigSchemes.groovy:35)
I also tried using the FieldLayoutManager instead and here is the code and error.
Code:
def temp= projectlessFieldConfigSchemes[0] as FieldConfigurationScheme
fieldLayoutManager.deleteFieldLayoutScheme(temp)
Error:
groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.issue.fields.layout.field.DefaultFieldLayoutManager.deleteFieldLayoutScheme() is applicable for argument types: (com.atlassian.jira.issue.fields.layout.field.enterprise.ImmutableFieldConfigurationScheme) values: [com.atlassian.jira.issue.fields.layout.field.enterprise.ImmutableFieldConfigurationScheme@#############]

4 answers

0 votes
Volodymyr April 1, 2024

Script for deleting not used Field Configuration Schemes and Field Configurations. Please use responsibly:

import com.atlassian.jira.component.ComponentAccessor

def html = ''

def flm = ComponentAccessor.getFieldLayoutManager()

// Field Configuration Schemes without projects  - can be deleted

def schemesToDelete = flm.getFieldLayoutSchemes().findAll{!it.getProjectsUsing()}

html +="Field Configuration Schemes for deletion(count:${schemesToDelete.size()}):"+schemesToDelete*.name+'<br>'

//delete not used schemes

schemesToDelete.each{ scheme ->

   // flm.removeFieldLayoutScheme(scheme)

   html += "Scheme: ${scheme.name} was deleted<br>"

}

// part 2: deletion of Field Configurations

def layouts = flm.getEditableFieldLayouts()

// Field Configurations without Field Configuration Schemes - can be deleted

def fieldConfigurationsToDelete = layouts.findAll{!flm.getFieldConfigurationSchemes(it)}

html +="Field Configurations for deletion(count:${fieldConfigurationsToDelete.size()}):"+fieldConfigurationsToDelete*.name+'<br>'

//delete not used Field Configurations

fieldConfigurationsToDelete.each{ config ->

    //flm.deleteFieldLayout(config)

    html += "Config: ${config.name} was deleted<br>"

}

return html
0 votes
Jeremy Jedlicka June 15, 2023

This was a very confusing thread to follow. For anyone seeing this in the future here is the code to delete a set of field configurations by their ID.

 

 

import com.atlassian.jira.component.ComponentAccessor

def fieldConfigSchemeMgr = ComponentAccessor.fieldLayoutManager

def layouts = []

for (l in layouts) {

def layout = fieldConfigSchemeMgr.getFieldLayout(l)

fieldConfigSchemeMgr.deleteFieldLayout(layout)

}
Stephanie Bryant August 4, 2023

Jeremy,

Thanks for this code snippet! Is this to delete a field configuration scheme, or just the field configuration/layout?

Volodymyr April 1, 2024

Discussion helped me to write script for deleting not used Field Configuration Schemes and Field Configurations. Adding as a separate answer

0 votes
KD November 7, 2022

You can delete the schemes in this way.

def fieldLayoutManager = ComponentAccessor.getFieldLayoutManager()
def temp = fieldLayoutManager.getFieldLayoutSchemes()
fieldLayoutManager.removeFieldLayoutScheme(temp[1])
Anyone looking should be able to extrapolate what they need into an if and then an each. Best of luck to anyone trying to understand the difference between
fieldConfigSchemeMgr.getFieldConfigScheme(x)
and
fieldLayoutManager.getFieldConfigurationScheme(x). Unfortunately, that answer was not found in this discussion.
Michael Aglas June 7, 2023

fieldConfigSchemeMgr.getFieldConfigScheme will get you the issuetype scheme, while the other gets you the field configuration scheme -> don't know why the naming is so weird in the API

0 votes
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 2, 2022

Welcome to the Atlassian Community!

Immutable objects do not have function calls that change them, so your second try is always going to fail.

In your fist bit of code, I don't think your temp object contains anything.

KD November 2, 2022

Thank you for your interest. So the first bit of code is referring to an array of Field Configuration Schemes that I gathered via the command

fieldConfigMgr.getFieldConfigurationSchemes(fieldLayout)
So I can with certainty say that there are Field Configuration Schemes in the array. You're right that it's Immutable, but I'm not sure how to call or if it is possible to call a mutable field config scheme to make the second code set work ever.

It's probably important to note that this code is being run via the scriptrunner console.
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 2, 2022

But you're not putting anything into the "temp" variable, unless you've not given us all the code.

KD November 3, 2022

I did not, just put in the lines that led to the error since the full code is quite long and unnecessary. I have gone ahead and simplified the code with the error to be self-contained. FCS 13300 exists. I am certain there is something small I am missing, but can't find any working examples online either.

 

import com.atlassian.jira.component.ComponentAccessor
def fieldConfigSchemeMgr = ComponentAccessor.fieldConfigSchemeManager

fieldConfigSchemeMgr.removeFieldConfigScheme(13300)
KD November 3, 2022

Very interesting development this: 

fieldConfigSchemeMgr.getFieldConfigScheme(13300)
Returns null! So you're definitely onto something of me grabbing nothing. But get this, if I grab it like this:
fieldLayoutManager.getFieldConfigurationScheme(13300)
I get the scheme I'm looking for. Why is it not finding this?
I looked at the groovy documentation and removeFieldConfigScheme(Long fieldConfigSchemeId) vs getFieldConfigurationScheme(Long schemeId)

What would the difference be in fieldConfigSchemeId and schemeId? Maybe this base misunderstanding is the root of my issue?
KD November 3, 2022
getFieldConfigScheme() returns issue type schemes! Why?! Groovy, why.
KD November 3, 2022

I still would love some insight into this as you would think that the method "getFieldConfigScheme" would return a Field Config Scheme, but I have gotten the code to work with

def fieldLayoutManager = ComponentAccessor.getFieldLayoutManager()
def temp = fieldLayoutManager.getFieldLayoutSchemes()
fieldLayoutManager.removeFieldLayoutScheme(temp[1])
But please still please point me in the direction of any good documentation on this because I was not able to find any and would truly like to understand the "why" behind this.
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 3, 2022

I am very confused by your postings of partial bits of code, so I am not sure what you would like a pointer to. 

But I suspect it would be a part of https://docs.atlassian.com/software/jira/docs/api/9.3.1/

KD November 7, 2022

Nope, that's the documentation I referred to earlier that was not great, but I have answered the based question for myself and posted the code for others, so I'll go ahead and close the question.

Suggest an answer

Log in or Sign up to answer