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 Discourse stores deleted topics? I can still access topics with direct link.

The topics remain in the database forever. Only staff will still be able to see deleted topics

How can I delete topics from the database?

Topics cannot be deleted from the database.

Is it be possible to restrict the topic deletion to a time period (between two dates)?

You’d need do it from the rails consoles, a command similar to the following should work:

 Topic.where("created_at.....").destroy_all

Is category ID the position on the categories page?

The category ID is what you’d see if you do

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 Likes