Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Bulk-Delete Comments

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

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

 

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

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?

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

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?

Yes, you can.

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

issueKeys.each{ issueKey ->

MutableIssue ...

}

THX... it works fine :-)

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

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!

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

Hi Henning,

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

IssueManager and CommentManager?

Thanks

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 

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

the change history gets lost...

0 votes

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.

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.

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

Nevermind. I'm good. Thanks. 

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

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 _TMC_ likes this

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.

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

Like Mark de Bont _TMC_ likes this

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

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

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.

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)
    }
}

Use Python (for free) instead...

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

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you