Delete thousands of issues created by email loop
Added by Claudio Sakaguti 3 months ago
Hello!
I have a problem. An email loop created thousands of issue at Redmine.
They all have the same subject "Undelivered Mail Returned to Sender".
Can somebody please teach me how can I bulk delete these issues?
Replies (4)
RE: Delete thousands of issues created by email loop - Added by Claudio Sakaguti 3 months ago
Sorry, I forgot to inform my details:
System info: Linux redmine01011 6.1.0-25-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.106-3 (2024-08-26) x86_64 GNU/Linux
Uptime: 3 days
Redmine Plugin Kit: 1.0.5
Environment:
Redmine version 5.1.3.stable
Ruby version 3.1.6-p260 (2024-05-29) [x86_64-linux]
Rails version 6.1.7.8
Environment production
Database adapter Mysql2
Mailer queue ActiveJob::QueueAdapters::InlineAdapter
Mailer delivery smtp
Redmine settings:
Redmine theme Default
RE: Delete thousands of issues created by email loop - Added by Lorenzo Meneghetti 2 months ago
Hi, such a things should be done from rails console from an administrator or with a bulk delete, for example from rails console with a single line of code like:
Issue.where(subject: 'Undelivered Mail Returned to Sender').map(&:destroy)
!not tested!... anyway the way is this one!
RE: Delete thousands of issues created by email loop - Added by Claudio Sakaguti 2 months ago
Hello Lorenzo!
Thanks for your reply.
Sorry, I forgot to update this issue, but a friend of mine found the solution and we where able to delete the more than 100 thousand tickets. Here is the code for reference.
=============
day_before_incident = Date.parse("2024-10-01")
batch_size = 1000
Issue.where("subject = 'Undelivered Mail Returned to Sender' and DATE > ?", day_before_incident).find_in_batches(batch_size: batch_size) do |group|
group.each(&:destroy)
end
============
The incident day was October 2.
The script above will search for issues created after the day_before_incident with subject "Undelivered Mail Returned to Sender" and delete them in batches of a thousand issues.
RE: Delete thousands of issues created by email loop - Added by Claudio Sakaguti 2 months ago
This is my friend's full email:
Further down in the discussion, someone gives instructions that work.
I tested by creating these tickets at qa:
http://192.168.4.232/redmine/issues/100898
http://192.168.4.232/redmine/issues/100899
Then I entered the interactive ruby (irb) console:
bitnami@debian:~$ cd /opt/bitnami/redmine
bitnami@debian:/opt/bitnami/redmine$ RAILS_ENV=production bin/rails console
Loading production environment (Rails 6.1.7.8)
Then in the console you can inspect the tickets.
First you establish the day_before_incident (must be UTC).
irb(main):001:0> day_before_incident = Date.parse("2024-10-01")
=> Tue, 01 Oct 2024
Then to check the number of tickets created by mistake:
irb(main):002:0> Issue.where("subject = 'Undelivered Mail Returned to Sender' and DATE > ?", day_before_incident).count
=> 2
Then to get details about the tickets (limiting to at most 10 tickets):
irb(main):003:0> Issue.where("subject = 'Undelivered Mail Returned to Sender' and DATE > ?", day_before_incident).limit(10).pluck(:id, :subject, :created_on)
=> [[100898, "Undelivered Mail Returned to Sender", Wed, 02 Oct 2024 21:30:27.000000000 UTC +00:00], [100899, "Undelivered Mail Returned to Sender", Wed, 02 Oct 2024 21:46:15.000000000 UTC +00:00]]
In the above I got the two tickets I created (100898 and 100899) so it is OK.
As a double check, we can remove the check for field created_on and we will get all tickets including old tickets not created by the incident:
irb(main):004:0> Issue.where("subject = 'Undelivered Mail Returned to Sender'").limit(10).pluck(:id, :subject, :created_on)
=>
[[40013, "Undelivered Mail Returned to Sender", Mon, 31 Jan 2022 13:04:13.000000000 UTC +00:00],
[55423, "Undelivered Mail Returned to Sender", Wed, 26 Oct 2022 18:11:13.000000000 UTC +00:00],
[64177, "Undelivered Mail Returned to Sender", Fri, 10 Mar 2023 17:39:12.000000000 UTC +00:00],
[64284, "Undelivered Mail Returned to Sender", Mon, 13 Mar 2023 11:05:13.000000000 UTC +00:00],
[64751, "Undelivered Mail Returned to Sender", Fri, 17 Mar 2023 18:12:14.000000000 UTC +00:00],
[100898, "Undelivered Mail Returned to Sender", Wed, 02 Oct 2024 21:30:27.000000000 UTC +00:00],
[100899, "Undelivered Mail Returned to Sender", Wed, 02 Oct 2024 21:46:15.000000000 UTC +00:00]]
irb(main):005:0>
Then since we confirmed we can get the tickets of the incident now we can change the command to destroy the tickets.
We can use
limit(NUMBER_OF_TICKETS_TO_DESTROY)
to destroy them in batches this way:
Issue.where("subject = 'Undelivered Mail Returned to Sender' and DATE > ?", day_before_incident).limit(1).each(&:destroy)
(the above will destroy only one ticket).
But as it was said in the thread:
Just be warned, this is a resource intensive process and will bog down your server, but it properly clears things up and is the same action as the "Delete" action that is performed via the Redmine web interface
I was thinking deleting the records at db would be enough as there are no notes, relations, attachments etc.
But ChatGPT explained there are things like history entries etc and so just deleting the issues records will not be enough.
https://chatgpt.com/share/66fdca1e-5ca4-8005-b539-d5d05d9e1af4
So adjusting the instructions and deleting 1000 issues at a time we would do:
day_before_incident = Date.parse("2024-10-01")
batch_size = 1000
Issue.where("subject = 'Undelivered Mail Returned to Sender' and DATE > ?", day_before_incident).find_in_batches(batch_size: batch_size) do |group|
group.each(&:destroy)
end