Hi, I want to delete a filter using mysql query... Edited

Hi, I want to delete a filter using mysql query... Even though it is deleting from database then filter still shows in  Favorite Filters. When I click on Admin clog and select delete it says it is already deleted and removed from the favorite lists. But I want that to happen automate this thing. How to resolve this issue.


import groovy.sql.Sql
import java.sql.*
import org.apache.log4j.Logger
import org.apache.log4j.Level
import com.atlassian.jira.ComponentManager
import com.atlassian.mail.Email
import com.atlassian.mail.server.MailServerManager
import com.atlassian.mail.server.SMTPMailServer
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser

def log = Logger.getLogger("test.connector")

def driver = Class.forName('com.mysql.jdbc.Driver').newInstance() as Driver

def props = new Properties()
props.setProperty("user", "xxxx")
props.setProperty("password", "xxxxx!")

def conn = driver.connect("xxxxxx/jiradb?useUnicode=true&characterEncoding=UTF8&sessionVariables=storage_engine=InnoDB", props)
def sql = new Sql(conn)
try {

List<String> filternames=[];
List<String> filtername_username=[];
List<String> filterid=[];
def usermanager = ComponentAccessor.getUserManager()
def flag = false

// returns if there is a empty filter
sql.eachRow("select * from searchrequest where reqcontent = ''") {row ->
log.debug("Empty Filter Name: "+row.filtername)


//executes if there is a empty
ComponentManager componentManager = ComponentManager.getInstance()
SMTPMailServer mailServer = ComponentAccessor.getMailServerManager().getDefaultSMTPMailServer()

for(int j=0;j<filternames.size();j++){
String table="";
def filter_user = usermanager.getUserByKey(filtername_username[j])
table+="Filter ID: "+filterid[j]+" FilterName: "+filternames[j]+ " Created By : "+filtername_username[j]+ " \n"
table+= "\n \n Hello "+filter_user.getDisplayName()+", \n \n You have created a empty filter named "+filternames[j]+" which will impact the jira performance. So, We are removing it. Please create a filter with some query in it.\n"
table+= "\n\nThanks and Regards, \nJira Admins \n \n"

//deleting from favoritesassociations
sql.execute("delete from favouriteassociations where entitytype = 'SearchRequest' AND entityid = "+filterid[j]+"")


//deleeting from searchrequest
sql.execute("delete from searchrequest where reqcontent = '' and username = '"+filtername_username[j]+"' and filtername='"+filternames[j]+"'");
log.debug("filter deleted")
flag = false
catch (Exception e) {
log.debug("filter not deleted");
flag = true

if (mailServer&&flag==false) {
Email email = new Email(filter_user.getEmailAddress())
email.setSubject("Empty Filter Alert ")
String content = table
log.debug("Email Sent")
List<String> admins = ["xxx","xxxx","xxxx"]

for(int i=0;i<admins.size();i++){
SMTPMailServer mailServer1 = ComponentAccessor.getMailServerManager().getDefaultSMTPMailServer()
def admin = usermanager.getUserByKey(admins[i])
def admin_msg=""
admin_msg+= "Hello "+admin.getDisplayName()+" , \n \n"
admin_msg+="We have a empty filter named '"+filternames[j]+"' created by '"+usermanager.getUserByKey(filtername_username[j]).getDisplayName()+"'("+filtername_username[j]+"). We have automatically deleted it and notified the user. \n \n "
admin_msg+="Thanks & Regards, \n"
admin_msg+="Jira Admins"
Email adminemail = new Email("xxxxxxxx")
adminemail.setSubject("Automatic Deletion of Empty Filter Alert -- JIRA TEST ")
String admincontent = admin_msg
log.debug("Admin Email Sent")
catch (Exception e) {
log.debug("admin not found");

else {
log.error("No SMTP mail server defined")


} finally {

1 answer

1 accepted

0 votes
Accepted answer

Directly modifying the SQL database while Jira is running is not advised.   This is because there are a number of cached elements from the database that are loaded up when jira is started.  For you to change the SQL database without Jira knowing about this is the reason for leaving this 'favorite filter' artifact around.

There are two possible ways to get around this:

  1. Make SQL changes only when Jira is stopped.  This way when Jira starts up it can cache that data correctly and accurately. (provided that your SQL query is the same way Jira would delete this information from the database)
  2. Make these changes via the Jira REST API.


I would recommend using method #2, just because you don't have to stop Jira this way.  You can see how to do this via the rest API in DELETE /rest/api/2/filter/{id}.

I did it using groovy api. It is working fine now. Thanks.

@Andrew Heinzer Can we use rest api for deleting private filters also ? 

How can we delete private filters ?  . I am not even able to access them using rest api .

@DTS Infra Admin No you cannot.  The REST API will only let you delete filters that either you created or that have been shared with you as an editor.  Starting with Jira 7.12 there have been some expanded abilities for end users to quickly share their filters with other users to view and edit.  More details in the Jira Software 7.12 release notes

But for filters that have not been shared with other users, Jira is designed to keep those filters private.   Even Jira admins aren't able to edit/manage these in the web UI nor via the REST API.  There is an existing feature request over in https://jira.atlassian.com/browse/JRASERVER-41269 that requests that specific feature and has been looking like it might actually get more attention in the near future.

Suggest an answer

Log in or Sign up to answer
Community showcase
Published 2 hours ago in Jira

How you can achieve compact and easy-to-maintain workflows in your JIRA( Server)

This approach requires you to have the JIRA administrative rights. The main aim of this article is to help you achieve an organized, easy-to-maintain workflows in your JIRA instance thereby, reducin...

31 views 0 0
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you