Mass close existing topics older than x?

You can find the category ID, then use it as an additional condition in the command. For example, close all open topics in the ‘general’ category that were created prior to September 24th:

cat_id = Category.find_by_slug('general').id
Topic.where(closed: false).where("created_at < '2024-09-24'").where(category_id: cat_id).find_each do |topic|
  topic.update_status('closed', true, Discourse.system_user)
end

These types of operations always make me a little nervous. Be sure to create a backup of your database before running them in case something goes wrong. It might also be a good idea to run some kind of preliminary check to make sure that you are operating on the correct data. One way to do that would be to return the count of topics that the operation will be performed on. For example:

Topic.where(closed: false).where("created_at < '2024-09-24'").where(category_id: cat_id).count
4 Likes