Deleting all topics in a category

To delete all topics from a category, you will need access SSH access to your server. If you use managed Discourse hosting, get in touch with your provider and ask them to help. If you run your own server, here are the steps you need to take to delete all topics in a category:

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

  1. Find the “slug” of your category. This is the name of category used in the URL.

  2. SSH into the server

  3. cd /var/discourse
    
  4. ./launcher enter app
    
  5. rake destroy:topics["mycategory"]
    

    (replace “mycategory” with the slug of your category)

  6. Wait… this may take some time, depending how many topics you have

Subcategories

To delete topics in a subcategory, you need to supply the parent category slug as well. For example, if “childcategory” is a subcategory of “parentcategory”, the command would look like

rake destroy:topics["childcategory","parentcategory"]

:warning: All categories

To delete all topics in all categories, you can use the command:

rake destroy:topics_all_categories
13 Likes

Thanks, this was a brilliant tip.

How long Discourse stores deleted topics? I see it still become accessible with direct link.

The topics remain in the database forever

Only staff can see deleted topics.

4 Likes

How can I delete them from the database?

Edit it, obfuscate it. But you can’t delete it.

Hi, would it be possible to restrict this instructions to a time period (between two dates)?

1 Like

You’d do it from the rails console. You can likely search here for something like

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

Is there are quick way to delete all topics/subcategories in a category, or do they have to be done one by one?

I have some categories with 20+ subcategories.

1 Like

This should be what you are looking for:

As always, I recommend taking a backup before working with these kind of destructive tasks :slight_smile:

5 Likes

Thank you. Is category ID the position on the categories page?

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

rake categories:list
4 Likes

Is there a way to do this through the API? I’ve tried using an approach where I:

  1. Find all the topics for a category:
page = 0
loop do
  response = @client.get("/c/#{topic_slug}/l/latest?page=#{page}")
  all_topics += response.body["topic_list"]["topics"] if response.body["topic_list"]["topics"].present?
  page += 1
  break if response.body["topic_list"]["topics"].blank?
end
return all_topics
  1. Find all the posts for each topic:
response = @client.get("/t/#{topic_id}/post_ids")
post_ids += response.body["post_ids"] if response.body["post_ids"].present?
  1. Try to delete all the posts for each topic. This is where I run into some kind of permissions error. The first way I tried was to use the “destroy_many” route:
      query_string = {post_ids: post_ids}.to_query
      @client.delete("/posts/destroy_many/#{query_string}")

Which resulted in this Error message:

DiscourseApi::UnauthenticatedError ({“errors”=>[“You are not permitted to view the requested resource.”], “error_type”=>“invalid_access”})

So, I tried deleting each individually, using the Ruby Gem API:

post_ids.map {|post_id| @client.delete_post(post_id)}

I found that I am permitted to delete all the posts, except the “About the {foo} category…” post that is automatically created by the system when a category is first created.

Does anyone know how I can delete the system created “About the {foo} category” post using the API?

No, that topic is required, as it contains the category description.

Thanks Jeff! What I actually discovered is that I can delete the category if I

  1. Find all the topics for a category
  2. Delete all topics, except the “About {foo} category” topic, which always has the numerically lowest topic_id for a category.
  3. Delete the category.

Thank you SO MUCH for your assistance. You guys are amazing and Discourse is amazing!

3 Likes