Hi,
I have migrated a forum from XenForo to Discourse, now there are many old posts that I would like to close. Is there a way to say “close all topics older than x months”? I have already set this in the category, but it only works for new topics.
The discussion revolves around closing old topics in a Discourse forum migrated from XenForo. Tealk asks how to close all topics older than a certain date, noting that category settings only apply to new topics. putty suggests using advanced filters and bulk actions, while Moin points to bulk action guides. Tealk references a Ruby script to close topics created before a specific date. simon explains how to limit the script to a specific category by using the category ID and advises running a count check first to ensure accuracy, emphasizing the need for a database backup before executing such operations.
Hi,
I have migrated a forum from XenForo to Discourse, now there are many old posts that I would like to close. Is there a way to say “close all topics older than x months”? I have already set this in the category, but it only works for new topics.
You can close topics in bulk with Performing bulk actions as a moderator
But depending on how many topics you want to close, Administrative Bulk Operations may be the better choice.
Yes, there would be something similar linked, how can I narrow it down to one category?
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