# Delete deleted-posts permanently in bulk?

**URL:** https://meta.discourse.org/t/delete-deleted-posts-permanently-in-bulk/203289
**Category:** Support
**Created:** [September 12, 2021, 4:15am UTC](https://meta.discourse.org/t/delete-deleted-posts-permanently-in-bulk/203289 "2021-09-12T04:15:12Z")
**Posts on this page:** 1
**Showing post:** 45

<div class="post-metadata">

### Author: ![nathank](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nathank/32/290039_2.png) [@nathank](https://meta.discourse.org/u/nathank)
#### Post date: [November 27, 2021, 2:12am UTC](https://meta.discourse.org/t/delete-deleted-posts-permanently-in-bulk/203289/45 "2021-11-27T02:12:28Z")

</div>

Well, thanks to @pfaffman I have identified the relevant posts using this:

```plaintext
Post.find_by_sql("select id from posts where topic_id not in (select id from topics)")

```

I get this output:

> [1] pry(main)\> Post.find\_by\_sql(“select id from posts where topic\_id not in (select id from topics)”)  
> =\> [#\<Post:0x000055df30d4ee90 id: 150\>,  
> #\<Post:0x000055df2e538ff0 id: 51097\>,  
> #\<Post:0x000055df2e50ba28 id: 83\>,  
> #\<Post:0x000055df2e4ee8b0 id: 40636\>,  
> #\<Post:0x000055df2e4a92d8 id: 62562\>,  
> #\<Post:0x000055df2e4b7978 id: 13522\>,  
> etc

However, I cannot work out how to then apply `destroy_all` to this selection.

This might help (for my own benefit):

> **[The Where Subquery - Advanced Active Record: Using Subqueries in Rails](https://pganalyze.com/blog/active-record-subqueries-rails#the-where-subquery)**
>
> Active Record provides a great balance between the ability to perform simple queries simply, and also the ability to access the raw SQL sometimes required to get our jobs done. In this article, we will see a number of real-life examples of business...

Any suggestions?

---

_[View the full topic](https://meta.discourse.org/t/delete-deleted-posts-permanently-in-bulk/203289)._
