How to bulk delete Inactive/Draft workflows

Lokesh D January 18, 2012

Need to delete more than 100 Inactive/Draft workflows.

Wondering if there is there a automated way to achieve this, via any scripting methods ?

5 answers

17 votes
Jeff Ward February 23, 2016

If you have the ScriptRunner plugin, the following script will delete all inactive workflows for you, ignoring the "classic' workflow.

import com.atlassian.jira.component.ComponentAccessor

def workflowManager = ComponentAccessor.workflowManager
def schemeManager = ComponentAccessor.workflowSchemeManager

def sb = new StringBuffer()

workflowManager.workflows.each {
if(!it.systemWorkflow) {
def schemes = schemeManager.getSchemesForWorkflow(it)
if (schemes.size() == 0) {
sb.append("Deleting workflow: ${it.name}\n")
workflowManager.deleteWorkflow(it)
}
}
}
return sb.toString()

 

Note this will not delete inactive schemes, only inactive workflows.  Modifying this to find inactive schemes should be possible.

Yevhen L March 5, 2016

A quick note: system default workflow should be assigned to some scheme, otherwise script won't work.

Like # people like this
Ansar Rezaei
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.
August 20, 2016

Thank you so much.
It work for me using Yevgen suggestion.

 

David Skreiner
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.
October 4, 2016

Unfortunately ScriptRunner is no longer free, but has gone commercial / expensive, so many people can't follow the suggestion. 

 It's frustrating to Admins and harmful to Atlassian's reputation that so many problems (including some horrible spots in the UI, such as this Delete Workflow UI or the user picker) can only be solved by spending a lot of money on plugins.

Like # people like this
Thomas Bores March 9, 2017

I definitively agree with you!

Like Ian T Price likes this
Deepa Tantry June 27, 2017

Hi Jeff, 

This is of really good help! Thank you!

Can you please let me know if using the below line instead of delete phrase in the script would help me just list the inactive workflows?

 

workflowManager.findWorkflow(it)

 

Thanks

 

Like Sundareswaran K likes this
Bill Neff September 29, 2017

The following snippet should work to delete all inactive workflow schemes.  Coupled with the script above, it should clean things up:

 

import com.atlassian.jira.component.ComponentAccessor

def schemeManager = ComponentAccessor.workflowSchemeManager

def sb = new StringBuffer()

schemeManager.schemeObjects.each {
try{
if(schemeManager.getProjectsUsing(schemeManager.getWorkflowSchemeObj(it.id)).size() == 0) {
sb.append("Deleting workflow scheme: ${it.name}\n")
schemeManager.deleteScheme(it.id)
}
}
catch(Exception e) {
//noop
sb.append("Error: " + e + "\n");
}
}

return sb.toString()
Like # people like this
Nabil Assi October 19, 2017

Both scripts work like a charm, cleaning up the workflows and the Worflow Schemes.

For some reason one of my workflows cannot be deleted. The logs shows that is it assigned to a scheme eventhough it`s not anymore. So i had to add the "catch exception" to the script to bypass it.

Tej January 30, 2018

Hey Bill,

I'm trying to edit these excellent scripts to bulk delete other schemes like issue type, issue type screen scheme, got stuck, can you plz help with other bulk delete scripts too

https://community.atlassian.com/t5/Adaptavist-questions/How-to-bulk-delete-screen-schemes-or-issue-type-schemes/qaq-p/712428

Thank you

Bill Neff January 30, 2018

Scripts for deleting unused Issue Type Screen Schemes and Screen Schemes are here: https://community.atlassian.com/t5/Adaptavist-questions/How-to-bulk-delete-screen-schemes-or-issue-type-schemes/qaq-p/712428

Like Ahsan likes this
Tej February 9, 2018

Hey Jeff Ward 

When I tested bulk delete inactive workflows, got this error

Associated default workflow to other workflow schemes too

Workflow error.png

Jeff Ward April 17, 2018

I just got this as well. I've modified the answer to avoid looking at schemes for the system workflow.

Raju Adluru August 1, 2018

Hi Jeff

Can this script prompt for me to delete or not to delete?

or can i put a criteria to delete only old inactive workflows which are modified in 2016, 17, not in 2018?

Thanks for your help.

Corentin Bresson July 18, 2019

Hi Jeff,

Thanks for your script.

To be be precise, it will delete workflows that are not associated with any Workflow Scheme.

A workflow that is associated with a Workflow Scheme will not be deleted, even though the Workflow can be inactive, and appear as such on http://<jira>/secure/admin/workflows/ListWorkflows.jspa .

This is because the Workflow Scheme(s) that a Workflow is associated with can be itself active (ie associated with a project) or inactive (not associated with any project.

Like Masco likes this
Ahsan September 1, 2019

Is there any script to delete unused Field Configuration Schemes, Field Configurations and Fields?

Also, Unused Issue Types Schemes and Issue Types.

Thanks in advance. 

11 votes
Kyle
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 8, 2019

For those of you who would like to avoid buying ScriptRunner just for this clean up task, have a try with this awesome solution: https://gist.github.com/zionyx/378ba519239bf040b2a6310b68d2740d

You are most welcome. :)

Ahmad July 3, 2019

you're awesome!! 

Vitali Ermakov July 15, 2019

Works fine with Safari. 

Mason Crouse June 9, 2020

Worked for me in Chrome

Kirk Garcia January 26, 2021

perfectly works - thanks for the suggestion.

Alex May 28, 2021

Thanks a lot. Works in Opera.

Maksim Beliaev January 27, 2023

brilliant!

1 vote
jv_sogorb December 10, 2019

I have created a simple script that you can execute it in chrome. No need to install or pay anything.

Execute it until you get rid of all deleteable workflows.


function
 deleteNext(){    waitAndClick('#inactive-workflows-module-heading h3', () => {        waitAndClick('[data-operation="delete"]', () => {            waitAndClick('[name="Delete"]')        })    })    document.querySelector('[data-operation="delete"]').click()    document.querySelector('[name="Delete"]').click()}
function waitAndClick(selectorcallback) {    const el = document.querySelector(selector);    if (el) {        el.click();        return (callback? callback() : null;    }    return setTimeout( () => {        waitAndClick(selectorcallback);    }, 100);}
deleteNext()
Nika February 20, 2020

Got Syntax error 

Joe Doperalski August 6, 2020

nice.  I needed to add some semi colons...

function deleteNext(){
waitAndClick('#inactive-workflows-module-heading h3', () => {
waitAndClick('[data-operation="delete"]', () => {
waitAndClick('[name="Delete"]');
})
})
document.querySelector('[data-operation="delete"]').click();
document.querySelector('[name="Delete"]').click();
}
function waitAndClick(selector, callback) {
const el = document.querySelector(selector);
if (el) {
el.click();
return (callback) ? callback() : null;
}
return setTimeout( () => {
waitAndClick(selector, callback);
}, 100);
}
deleteNext();

0 votes
cj little July 24, 2021

If you're running Jira Cloud and you'd like to delete all the workflows that Jira is willing to allow you to delete, then consider running the following script in ScriptRunner:

// Get a list of the first N Workflows (appears that N is max of 50)
Map<String, Object> searchResult = get('/rest/api/3/workflow/search')
.queryString("maxResults", "200")
.asObject(Map)
.body

def w = (List<Map<String, Object>>) searchResult.values


// Iterate over all the Workflows; Attempt to delete everyone :eek:
w.each {
if (it.id.entityId != null) {
println "Attempting to delete: ${it.id.entityId} - ${it.id.name}"
def hr = delete('/rest/api/3/workflow/'+ it.id.entityId).asString()
println hr
}
}

If you'd like to delete all the unreferenced Workflow Schemes, try this snippet:

// Get a list of the first 200 Workflow Schemes
Map<String, Object> searchResult = get('/rest/api/3/workflowscheme')
.queryString("maxResults", "200")
.asObject(Map)
.body

def ws = (List<Map<String, Object>>) searchResult.values

// Iterate over all the Workflow Schemes
ws.each {
// Only want the Workflow Schemes without any mappings
if (it.issueTypeMappings.size() == 0) {
println "Deleting: ${it.id} - ${it.name}"
def hr = delete('/rest/api/3/workflowscheme/'+ it.id).asString()
println hr
}
}

I've posted these in my GitHub Repo for forking and updating goodness.

I hope this saves you a couple hundred clicks, hours of your life, and puts a little smile on your face when you are making thankless changes cold and alone in the depths of Jira.  Please enjoy the hours this puts back in your life doing anything other than Jira Administration.  You've earned it.

0 votes
Eva
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.
February 8, 2012

Well I do it the way that Atlassian might not suggest: directly on the database itself. There is a table call Workflow_Draft (or something like that ) that I can just run a delete statement and then I just reindex the whole JIRA and restart the program. It works for me (in 4.4.1). But again, anything directly to database is not recommended, I guess I have just been lucky.

Suggest an answer

Log in or Sign up to answer