This guide provides instructions on how to bulk delete all topics within a category on a self-hosted Discourse instance.
Required user level: System Administrator
SSH access to your server is required
Removing all topics from a category can be necessary for various reasons, such as reorganizing content or clearing outdated discussions. This guide walks you through the steps to accomplish this task safely on a self-hosted Discourse server.
This is a destructive action. Make sure to back up your forum before continuing
Steps to delete topics in a category
- Identify the category slug: This is the name used in the category’s URL.
- Access your server: Use SSH to log into your server.
ssh username@your-server.com
- Navigate to the Discourse directory:
cd /var/discourse
- Enter the Discourse application:
./launcher enter app
-
Execute the deletion command: Replace
"mycategory"with your category’s slug.
rake destroy:topics["mycategory"]
This process may take time depending on the number of topics.
Deleting topics in subcategories
To delete topics in a subcategory, include the parent category’s slug in your command. For example:
rake destroy:topics["childcategory","parentcategory"]
Delete all topics in all categories
This action will clear all topics from every category!
rake destroy:topics_all_categories
Common Questions
How long does Discourse store deleted topics? I can still access topics with a direct link.
The topics remain in the database after deletion. Only staff will still be able to see deleted topics.
How can I permanently delete topics from the database?
You can permanently delete posts (and their topics) from the database using the destroy:posts rake task. This requires the hidden can_permanently_delete site setting to be enabled first. Pass post IDs as a comma separated list:
rake destroy:posts[4,8,15,16,23,42]
You can also pipe post IDs from a file:
cat post_ids.txt | rake destroy:posts
Is it possible to restrict the topic deletion to a time period (between two dates)?
You’d need to do it from the Rails console, a command similar to the following should work:
Topic.where("created_at.....").each do |t|
first_post = t.ordered_posts.first
PostDestroyer.new(Discourse.system_user, first_post).destroy if first_post
end
How do I find my category’s slug or ID?
The destroy:topics task uses the category slug (the name in the category URL). You can list all categories with their IDs and slugs by running:
rake categories:list
Additional Information
For more information about other administrative operations please see:
Last edited by @SaraDev 2024-11-07T20:50:52Z
Check document
Perform check on document: