# Deleting all messages from a User in a Category

**URL:** https://meta.discourse.org/t/deleting-all-messages-from-a-user-in-a-category/204899
**Category:** Support
**Created:** [September 30, 2021, 6:48pm UTC](https://meta.discourse.org/t/deleting-all-messages-from-a-user-in-a-category/204899 "2021-09-30T18:48:34Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Iceman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/iceman/32/181309_2.png) [@Iceman](https://meta.discourse.org/u/Iceman)
#### Post date: [September 30, 2021, 6:48pm UTC](https://meta.discourse.org/t/deleting-all-messages-from-a-user-in-a-category/204899/1 "2021-09-30T18:48:35Z")

</div>

Hi guys,

Recently I had the need to delete all posts from a user in a specific category. Leaving aside “debates” on deleting content I thought “oh, easy, just use the search and then pick all the results using the checkboxes and delete them”.

However, it seems that doing so allows me to delete **The Topics** and not so much the messages, even though the search is giving me the messages as results, not necessarily the topics (although they are there as well, yes).

Is there a “safe” way of doing this? At first I thought that I would be something very extreme to the DB (and I don’t discard that possibility) but since I can bulk-delete 50+ topics from the UI… maybe there is a way of doing that for messages only? (Or a way using the console, I just don’t want to “try” and blow up the DB 😛 )

Thanks!

---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [October 3, 2021, 6:59am UTC](https://meta.discourse.org/t/deleting-all-messages-from-a-user-in-a-category/204899/2 "2021-10-03T06:59:02Z")

</div>

I don’t have personal experience of these, but I believe you can ‘soft’ delete using the API -

> [@Reverse engineer the Discourse API](https://meta.discourse.org/t/how-to-reverse-engineer-the-discourse-api/20576):
>
> Discourse is backed by a complete JSON api. Anything you can do on the site you can also do using the JSON api. The API is documented at [docs.discourse.org](https://docs.discourse.org). You can also use the [discourse\_api](https://github.com/discourse/discourse_api) Ruby gem as a client library. However, not every endpoint is documented. To determine how to do something with the JSON API here are some steps you can follow. Example: recategorize a topic. Go to a topic and start editing a category: Open Chrome dev tools, switch to the Network tab, select …

> [@Iceman](#):
>
> (Or a way using the console, I just don’t want to “try” and blow up the DB 😛 )

And there are a few topics on ‘hard’ deleting if you search for ‘rails delete post’.

If you can give a few more details about what you’re trying to achieve, people may have more specific tips.

---

<div class="post-metadata">

### Author: ![Iceman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/iceman/32/181309_2.png) [@Iceman](https://meta.discourse.org/u/Iceman)
#### Post date: [October 3, 2021, 11:48pm UTC](https://meta.discourse.org/t/deleting-all-messages-from-a-user-in-a-category/204899/3 "2021-10-03T23:48:18Z")

</div>

Hi @JammyDodger

Thanks for the links.

> [@JammyDodger](#):
>
> If you can give a few more details about what you’re trying to achieve, people may have more specific tips.

I just want to delete all messages from a user in a specific category. 😛

PD: Maybe using the Console? I just don’t want to nuke my DB…

---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [October 22, 2021, 12:55pm UTC](https://meta.discourse.org/t/deleting-all-messages-from-a-user-in-a-category/204899/4 "2021-10-22T12:55:57Z")

</div>

> [@Iceman](#):
>
> PD: Maybe using the Console? I just don’t want to nuke my DB…

There’s some ongoing discussion about a similar thing in this topic: [Delete deleted-posts permanently in bulk?](https://meta.discourse.org/t/delete-deleted-posts-permanently-in-bulk/203289)

Hopefully that can provide some inspiration?

---

<div class="post-metadata">

### Author: ![Wingtip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/wingtip/32/102039_2.png) [@Wingtip](https://meta.discourse.org/u/Wingtip)
#### Post date: [April 11, 2025, 9:09pm UTC](https://meta.discourse.org/t/deleting-all-messages-from-a-user-in-a-category/204899/5 "2025-04-11T21:09:18Z")

</div>

Apologies for the necro, happy to create a new post if required.

We have a politics category and some users would like to delete all their posts from this category for obvious reasons. Is there any way to easily do this for them?

---

<div class="post-metadata">

### Author: ![Arkshine](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/arkshine/32/298682_2.png) [@Arkshine](https://meta.discourse.org/u/Arkshine)
#### Post date: [April 11, 2025, 11:53pm UTC](https://meta.discourse.org/t/deleting-all-messages-from-a-user-in-a-category/204899/6 "2025-04-11T23:53:42Z")

</div>

A solution can be to use the Rais console.

```plaintext
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`.

> ⚠ Please make a backup before any changes.

```ruby
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

```

---

<div class="post-metadata">

### Author: ![Wingtip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/wingtip/32/102039_2.png) [@Wingtip](https://meta.discourse.org/u/Wingtip)
#### Post date: [April 12, 2025, 3:56am UTC](https://meta.discourse.org/t/deleting-all-messages-from-a-user-in-a-category/204899/7 "2025-04-12T03:56:54Z")

</div>

Thanks! This is fantastic.
