# How to delete thousands of Personal Messages?

**URL:** https://meta.discourse.org/t/how-to-delete-thousands-of-personal-messages/162747
**Category:** Support
**Tags:** personal-messages
**Created:** [September 2, 2020, 7:12am UTC](https://meta.discourse.org/t/how-to-delete-thousands-of-personal-messages/162747 "2020-09-02T07:12:42Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![nildarar](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nildarar/32/331927_2.png) [@nildarar](https://meta.discourse.org/u/nildarar)
#### Post date: [September 2, 2020, 7:12am UTC](https://meta.discourse.org/t/how-to-delete-thousands-of-personal-messages/162747/1 "2020-09-02T07:12:42Z")

</div>

Our community has several thousand private messages that have been sent between users. Is there a way to completely remove private messages with more than 100 replies from the database (without recovery)?

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [September 2, 2020, 5:27pm UTC](https://meta.discourse.org/t/how-to-delete-thousands-of-personal-messages/162747/2 "2020-09-02T17:27:54Z")

</div>

Well, I think that this will do what you asked. It will delete any private messages that were not created by a system user or discobot. It will still delete any other private messages, including those from admins.

It’s not tested. I’m not saying that it’s a good idea. I won’t promise that it won’t do something bad.

```plaintext
cd /var/discourse
discourse backup
./launcher enter app
rails s
Topic.where(archetype: 'private_message').where("user_id > 0").destroy_all
exit
discourse enable_restore
discourse restore

```

If something bad does not happen, you can omit the last two steps. If something bad does happen, you’ll want to copy/paste the most recent backup printed by the `discourse restore` command to restore the backup.

---

<div class="post-metadata">

### Author: ![IAmGav](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/iamgav/32/235598_2.png) [@IAmGav](https://meta.discourse.org/u/IAmGav)
#### Post date: [September 2, 2020, 5:28pm UTC](https://meta.discourse.org/t/how-to-delete-thousands-of-personal-messages/162747/3 "2020-09-02T17:28:41Z")

</div>

You can remove all message at once.

> rake destroy:private\_messages

Reference:

> [@Administrative Bulk Operations](https://meta.discourse.org/t/administrative-bulk-operations/118349#heading--16):
>
> Below you will find a collection of bulk operations that can be initiated from the command line. You will need SSH access, so if you are a hosted customer, you will need to contact the Discourse team about running these commands. warning Before working with the console it is extremely important that you have a recent backup. Mistakes can always happen! First thing to do is enter your site’s container: cd /var/discourse ./launcher enter app Additional Guides: [Performing bulk actions a…](https://meta.discourse.org/t/performing-bulk-actions-as-a-moderator/272832)

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [September 2, 2020, 10:44pm UTC](https://meta.discourse.org/t/how-to-delete-thousands-of-personal-messages/162747/4 "2020-09-02T22:44:20Z")

</div>

Nice work, @IAmGav! I swear i looked there and didn’t see.

---

<div class="post-metadata">

### Author: ![nildarar](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nildarar/32/331927_2.png) [@nildarar](https://meta.discourse.org/u/nildarar)
#### Post date: [September 3, 2020, 4:40am UTC](https://meta.discourse.org/t/how-to-delete-thousands-of-personal-messages/162747/5 "2020-09-03T04:40:03Z")

</div>

> [@pfaffman](#):
>
> It’s not tested. I’m not saying that it’s a good idea. I won’t promise that it won’t do something bad.

Thanks Jay,  
Running batch delete query directly on the database is so Scary 😅

---

<div class="post-metadata">

### Author: ![nildarar](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nildarar/32/331927_2.png) [@nildarar](https://meta.discourse.org/u/nildarar)
#### Post date: [September 3, 2020, 5:04am UTC](https://meta.discourse.org/t/how-to-delete-thousands-of-personal-messages/162747/6 "2020-09-03T05:04:16Z")

</div>

I took a brief look at how this function works. As it turns out, after running it, all private messages will be deleted.

```
  def destroy_private_messages
    Topic.where(archetype: "private_message").find_each do |pm|
      @io.puts "Destroying #{pm.slug} pm"
      first_post = pm.ordered_posts.first
      @io.puts PostDestroyer.new(Discourse.system_user, first_post).destroy
    end
  end

```

I think I have to choose the scary way that @pfaffman pointed out to filter messages. or try to define custom rake function and use `PostDestroyer.new(Discourse.system_user, first_post).destroy`

---

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [November 26, 2021, 7:34am UTC](https://meta.discourse.org/t/how-to-delete-thousands-of-personal-messages/162747/7 "2021-11-26T07:34:35Z")

</div>

Revisiting this old topic, since none of the answers are 100% correct.

> [@nildarar](#):
>
> completely remove private messages with more than 100 replies from the database (without recovery)?

The rake task `rake destroy:private_messages` does **not** hard delete all private messages, it soft deletes them. They can still be seen by an admin and they can be recovered.

The Rails code `Topic.where(archetype: 'private_message').where("user_id > 0").destroy_all` destroys the topics, but not the actual messages. They are still in the database and can be viewed using for instance the [data explorer](https://meta.discourse.org/t/32566?silent=true) plugin.

This code _will_ permanently delete all private message posts and topics:  
**warning: sharp knife here!**

```plaintext
Post.where(topic_id: Topic.where(archetype: 'private_message').where("user_id > 0")).destroy_all
Topic.where(archetype: 'private_message').where("user_id > 0").destroy_all

```

(and to filter this on topics with 100 replies or more you need to extend the `Topic.where` with `where("posts_count > 100")` in both lines)
