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.

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Oct 16, 2018 in Jira

Looking for anyone who made the switch to Data Center

The Jira Marketing team is putting together an ebook on migrating to Data Center. We're looking for pro tips on how you staffed your project team and organized your Proof of Concept. Share yo...

1,393 views 17 10
Join discussion

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