Bulk delete all topics in a category

:bookmark: This guide provides instructions on how to bulk delete all topics within a category on a self-hosted Discourse instance.

:person_raising_hand: Required user level: System Administrator

:warning: 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.

:warning: This is a destructive action. Make sure to back up your forum before continuing

Steps to delete topics in a category

  1. Identify the category slug: This is the name used in the category’s URL.
  2. Access your server: Use SSH to log into your server.
ssh username@your-server.com
  1. Navigate to the Discourse directory:
cd /var/discourse
  1. Enter the Discourse application:
./launcher enter app
  1. Execute the deletion command: Replace "mycategory" with your category’s slug.
rake destroy:topics["mycategory"]

:information_source: 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

:warning: 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 documentPerform check on document:
21 לייקים