Script for deleting filters

Anuradha Yadav May 17, 2023

Hey,

We have some criteria for Filters to be deleted in our Jira Instance.
Could anyone help with Script in Scriptrunner for below?

We need to create a script that delete filters which fall under these criteria:

  1. filters to be deleted which are owned by inactive users, private and with fav count 0
  2. filters to be deleted which are owned by inactive users, private and with fav count 1
  3. filters to be cleaned up which have Owners that are inactive and Filters are not shared/not favorited by any User )
  4. private filters which is created by inactive users and Fav count <2. 
  5. private filters which is created by inactive users and Fav count 2 & more. 

or any idea is welcome.

Regards,
Anuradha

3 answers

1 accepted

0 votes
Answer accepted
CJ Edwards May 17, 2023

That is a pretty big ask.

I would start by reviewing this post

Jira questions Bulk-Delete-Issues-Using-ScriptRunner 

Work on a base from there and many of us can help beyond that.

Anuradha Yadav May 17, 2023

Hi @CJ Edwards ,
Thanks but the post you gave is for issues right? not for Filters.

I tried below Script:
But seeing some errors. Could you please help?

image.png

CJ Edwards May 17, 2023

Hi Anuradha,

I would work from the top of the script to narrow the problems.

-  getAllUsers() was deprecated long ago. Use UserSearchService.findUsers() use https://docs.atlassian.com/software/jira/docs/api/9.8.1/com/atlassian/jira/bc/user/search/UserSearchService.html

- for filter work use - com.atlassian.jira.bc.filter - Interface SearchRequestService

https://docs.atlassian.com/software/jira/docs/api/9.8.1/com/atlassian/jira/bc/filter/DefaultSearchRequestService.html

 

The error listed in your picture is from passing DelegatingApplicationUser instead of ApplicationUser that the function is expecting.

 

I hope this helps!

Like Anuradha Yadav likes this
0 votes
Rolf Lader
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 16, 2023

Hi folks

I have realized a similare case by using the following console script

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchRequest
import com.atlassian.jira.issue.search.SearchRequestManager
import com.atlassian.jira.user.ApplicationUser
import org.apache.log4j.Logger

Logger logger = Logger.getLogger("script.console")

/*
* This script removes shared filters using a list of filter ids (as long).
*/

//
// Define the 'manager(s)' before we can de configuration (!)
SearchRequestManager searchRequestManager = ComponentAccessor.getComponent( SearchRequestManager )

//
// Configuration: Define an array of all 'shared filter ids' which should be deleted.
long[] sharedFilterIDs = [17638, 18168, 18509, 18623]

// Remove all 'shared filters' if they does exist.
sharedFilterIDs.each { long sharedFilterID ->

SearchRequest sharedFilter = searchRequestManager.getSearchRequestById( sharedFilterID )
if ( sharedFilter != null ) {

// Delete the 'shared filter'
logger.debug "The filter '${sharedFilter.getName()}' (${sharedFilter.getId()}) owned by '${sharedFilter.getOwner().getDisplayName()}' is deleted."
searchRequestManager.delete( sharedFilterID )

// Now we check if the 'shared filter' has really been deleted.
if ( searchRequestManager.getSearchRequestById( sharedFilterID ) != null ) {
logger.error "The filter '${sharedFilter.getName()}' (${sharedFilter.getId()}) owned by '${sharedFilter.getOwner().getDisplayName()}' was not deleted!"
}

} else {
logger.error "The filter with the id ${sharedFilterID} does no more exist!"
}
}
logger.info "All shared filters (${sharedFilterIDs.size()}) are deleted."

 

Hope it helps you.

Regards, Rolf

0 votes
Henrique Bittencourt June 1, 2023

@Anuradha Yadav 

Did you have success using ScriptRunner to bulk delete filters?

I'm working on a project where I need to delete all the inactive users's filters or delete them by their filter id.

Suggest an answer

Log in or Sign up to answer