Bulk-Delete Comments

Stefan Niedermann July 24, 2013

Hi,

we had a buggy Jelly-Script which postet ten-thousands of comments on some issues.

Deleting them manually is not possible, every reload of the issue takes a few minutes.

The last chance i see is deleting them by a direct database-delete-statement.

Did anyone delete comments like this before or does anyone know if there are dependencies? or has anyone a jelly-script which is able to delete comments through api?

thanks & regards

12 answers

1 accepted

11 votes
Answer accepted
Henning Tietgens
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.
July 24, 2013

Deleting directly from the database is not recommended. But you can use the Script Runner plugin and the following script to delete all comments containing a specific text (in this case "Bla Bla Bla") in a specific issue (here TXS-561).

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.issue.comments.CommentManager

String issueKey = 'TXS-561'

IssueManager issueManager = ComponentAccessor.issueManager
CommentManager commentManager = ComponentAccessor.commentManager

MutableIssue issue = issueManager.getIssueObject(issueKey)
List<Comment> comments = commentManager.getComments(issue)
comments.each {comment ->
    if (comment.body.contains('Bla Bla Bla')) {
        commentManager.delete(comment)
    }
}

Henning

 

Tristan May 19, 2014

Just wanted to say this worked like a charm for a comment spam issue I had. Thanks!

Fabian Herz May 19, 2014

Thx for your script... I have many issue with the same comment and I will delete this comment. How can I include alle issue keys in this script?I will bulk delete this Comment?

Henning Tietgens
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 19, 2014

Take a look here on how to do a jql search. And than search for all issues and loop through the result.

Fabian Herz May 19, 2014

Ok, but I can't include the issue keys in your script? for example

String issueKey = 'xxx-685'; String issueKey = 'xxx-686' etc...

or with another separator?

Henning Tietgens
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 19, 2014

Yes, you can.

def issueKeys = ['xxx-456', 'aaa-333']

issueKeys.each{ issueKey ->

MutableIssue ...

}

Fabian Herz May 20, 2014

THX... it works fine :-)

Stefan Hett May 10, 2015

Thanks alot for this post @Henning Tietgens. This saved me a lot of work when cleaning-up the comments some spambot left on our JIRA instance over the weekend. I also took the time to write a quick How-To to apply your script to the JIRA instance: http://www.luke1410.de/blog/?p=29

Like Sameer.Khazi likes this
Daniel Tomberlin August 31, 2017

Hi @Henning Tietgens. I am on JIRA cloud and this script is giving me [x] errors to the left of all the imports at the top of your code before I even run it. The errors state "unable to resolve class com.atlassian.jira...(issue or component)" at each line (1-5). Am I missing something between the date of this post and now (a few years)?

Thanks!

Henning Tietgens
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.
September 1, 2017

Hi Daniel,

this is a restriction of JIRA Cloud plugins. See ScriptRunner package import restrictions

On JIRA Cloud the plugin (and therefore your script) is not able to directly interact with the JIRA system (see Script Runner Migration Guide for details), instead the REST API has to be used.

It should be possible to use https://docs.atlassian.com/jira/REST/cloud/#api/2/issue/{issueIdOrKey}/comment to accomplish the same thing as my script above, but I don't have a script ready for you.

Henning

gl autouser March 9, 2021

Hi Henning,

Can you specify the repository and dependency you are using for -

IssueManager and CommentManager?

Thanks

Henning Tietgens
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.
March 10, 2021

Hi,

In fact there are no special dependencies. If you open the Script Runner console, you can paste this code (containing less imports than the old one above):

import com.atlassian.jira.component.ComponentAccessor

String issueKey = 'TXS-561'

def issueManager = ComponentAccessor.issueManager
def commentManager = ComponentAccessor.commentManager

def issue = issueManager.getIssueObject(issueKey)
def comments = commentManager.getComments(issue)
comments.each {comment ->
if (comment.body.contains('Bla Bla Bla')) {
commentManager.delete(comment)
}
}

Best,
Henning 

1 vote
Cyril Egan
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.
July 24, 2013

Or clone the issue as cloning does not clone the comments. Then delete the original issue.

Stefan Niedermann July 24, 2013

the change history gets lost...

0 votes
Mark de Bont February 19, 2020

Use Python (for free) instead...

See my code snippet: https://bitbucket.org/snippets/markdebont/EbG7Xn

0 votes
Sreeraj Sreekumar October 2, 2019

Thank you so much Henning.

The above script worked for me in Jira 7.9.2 with slight syntax change (-&gt; replated with ->)

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.issue.comments.CommentManager

String issueKey = 'TXS-561'

IssueManager issueManager = ComponentAccessor.issueManager
CommentManager commentManager = ComponentAccessor.commentManager

MutableIssue issue = issueManager.getIssueObject(issueKey)
List<Comment> comments = commentManager.getComments(issue)
comments.each {comment ->
    if (comment.body.contains('Bla Bla Bla')) {
        commentManager.delete(comment)
    }
}
0 votes
CJ Edwards August 8, 2019

For anyone that this might help!

https://github.com/CJ-Edwards-QHR/bulk-delete-comments-jira/blob/master/groovy

(cant seem to post codeblocks?)

This is my naive approach to remove bulk/bad comments from across multiple issues in a project. It is not fast if you are searching a large project that is full of comments!!

This was based on code found at https://gist.github.com/lhotari/caafb4d394412f491861 which did not work for me at all.

0 votes
fty4 June 21, 2018

I created a python script to remove all comments for a specific Jira issue. 
It uses the API from Jira. Found it the easiest way without a plugin.

Here you go:
https://bitbucket.org/snippets/fty4/aeG4Bb/delete-all-jira-comments-from-issue

0 votes
Riley Major June 24, 2016

You can use the Chrome developer tools to run JavaScript to access the JIRA API. More details are available here:

http://stackoverflow.com/a/37950118/2266979

0 votes
Steffen Stamprath
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.
April 8, 2015
Andreas Haaken April 8, 2015

This is for Confluence.. not for Jira.. ;)

Like Mark de Bont likes this
0 votes
Lari Hotari July 29, 2014

I wrote a Jira Script Runner plugin Groovy script to delete Jira comment spam entered by a spambot that uses a username "Patrickmorton". See https://gist.github.com/lhotari/caafb4d394412f491861for the source.

0 votes
Richard Boult July 24, 2014

As a simple administrator, without access to scripts etc, how do I delete all comments from a page?

I've already copied page to retain snapshot with current comments.

Confluence 5.3

Bob Swift OSS (Bob Swift Atlassian Apps)
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.
July 24, 2014

This is a question about JIRA. You are asking about Confluence ... just create a new question so the answers will be for Confluence.

Like Mark de Bont likes this
0 votes
Bob Swift OSS (Bob Swift Atlassian Apps)
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.
July 25, 2013

It is not easy to put everything together, but with a little groovy scripting and JIRA Command Line Interface you could probably do it.

  1. getIssueList with JQL to find the issues affected - produces a CSV that has the issue's numeric id
  2. getCommentList for each issue affected - produces a CSV of comments that has the comment's numeric id
  3. renderRequest to delete the comment similar to this example
--action renderRequest --request "/secure/DeleteComment.jspa?id=108212&amp;commentId=48026&amp;Delete=Delete"

An easier alternative is available if you are really good at SQL against the JIRA database (and NOT OnDemand) to construct the right renderRequest (like above) for all the comments you need to remove. Then just use runFromSql to run all of them.

Tom McCallum March 5, 2019

Hi Bob. Huge fan, get your Friday emails, have a few of your plugins. I think I'm fairly good at SQL, I'm both our Oracle DBA and SQL Server DBA (jira is on MS-SQL) , and our Atlassian admin, but I haven't studied the Jira data model enough to know the tables or where clause. My case is very easy. We have a programmer that is basically 'pinging' SFDC, if he get's a success there, he writes a comment to a specific Jira issue, i.e., 'SFDCtest-123', but he wants me to do a cleanup of those comments every so often.

Do you know the SQL to delete all comments from a specific Jira issue id? delete * from whatever where jirakey = 'SFDCtest-123'?  I can try to reverse engineer, but what the heck, thought I might ask. 

-Tom McCallum

Tom McCallum March 5, 2019

Nevermind. I'm good. Thanks. 

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.
July 24, 2013

Your options are very limited here. To avoid loss of history and data, you're basically down to two options

- What Henning said - even the Jira CLI and Jelly can't delete the comments, the script runner pretty much is the only approach

- As you said, hack the database. You will need to stop Jira, get a backup, remove lines from "jiraaction table", start Jira and then update the affected issues index (either with a global index, or a bulk edit or comment on the ones you've changed).

I've had a similar problem in the past, and it is moderately safe as long as you do NOTHING else. But I never recommend database hacking, because it's so easy to get it wrong.

Suggest an answer

Log in or Sign up to answer