Automatically delete closed issues older than 365 days

Added by Krzysztof K 8 months ago

Hi, I need to automatically delete all closed issues from all projects older than 365 days from redmine. I need help with sql query becouse I'm not familiar with sql.

Environment:
Redmine version 4.0.5.stable
Ruby version 2.6.5-p114 (2019-10-01) [x86_64-linux]
Rails version 5.2.3
Environment production
Database adapter Mysql2
Mailer queue ActiveJob::QueueAdapters::AsyncAdapter
Mailer delivery smtp

Replies (2)

RE: Automatically delete closed issues older than 365 days - Added by Krzysztof K about 1 month ago

cd /opt/bitnami/redmine/bin
./rails c production
Issue.where("closed_on < '#{365.days.ago}'").delete

RE: Automatically delete closed issues older than 365 days - Added by Jim Lee about 1 month ago

One modification to the above suggestion...

You can use #Destroy to make sure that all callbacks for your issues are applied (things like clearing up issue relations, links and removing attachments from the file system). Delete is thousands of times faster, and does not use as many resources to perform, but you can leave your Projects and server in a very unhappy state. My suggestion is to do the following:

# RAILS_ENV=production bin/rails console
> p = Project.find("$project_name")
> p.issues.where("closed_on < '#{365.days.ago}'").each(&:destroy)
# To perform this actions across all projects...
> Issue.where("closed_on < '#{365.days.ago}'").each(&:destroy)

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

(1-2/2)