Deleting all messages from a User in a Category

A solution can be to use the Rais console.

cd /var/discourse
./launcher enter app
rails c

Then you can follow the following form by filling the category slug and username.
You copy the code and paste it into the console.

Note:

  • It doesn’t delete the first post to avoid the topic being deleted
  • If you target a subcategory, provide the parent, e.g.: parent_cat_slug/cat_slug.

:warning: Please make a backup before any changes.

Category
  .find_by_slug_path_with_id("=CATEGORY_SLUG=")
  .tap do |c|
    u = User.find_by_username("=USERNAME=")
    Post
      .joins(:topic)
      .where(user_id: u.id)
      .where("topics.category_id = ?", c.id)
      .where.not(post_number: 1)
      .where(deleted_at: nil)
      .find_each { |p| PostDestroyer.new(Discourse.system_user, p).destroy }
  end