# Bulk remove users

**URL:** https://meta.discourse.org/t/bulk-remove-users/53450
**Category:** Support
**Created:** [November 27, 2016, 10:32pm UTC](https://meta.discourse.org/t/bulk-remove-users/53450 "2016-11-27T22:32:00Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![yavuz](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/yavuz/32/121388_2.png) [@yavuz](https://meta.discourse.org/u/yavuz)
#### Post date: [November 27, 2016, 10:32pm UTC](https://meta.discourse.org/t/bulk-remove-users/53450/1 "2016-11-27T22:32:00Z")

</div>

I have imported my old data from phpBB3 which went smooth. But I made a mistake by not first cleaning up the old inactive users and still importing them. I now have around 4000 users with no posts I wish to remove.

What is the best way to bulk remove users?

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [November 28, 2016, 9:02am UTC](https://meta.discourse.org/t/bulk-remove-users/53450/2 "2016-11-28T09:02:25Z")

</div>

If you really don’t care about these users, then I would do this

1. make a backup (_better safe than sorry_)
2. ssh into your server and type

```plaintext
cd /var/discourse
./launcher enter app
rails c
User.where(post_count: 0).destroy_all

```

---

<div class="post-metadata">

### Author: ![yavuz](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/yavuz/32/121388_2.png) [@yavuz](https://meta.discourse.org/u/yavuz)
#### Post date: [November 28, 2016, 3:22pm UTC](https://meta.discourse.org/t/bulk-remove-users/53450/3 "2016-11-28T15:22:27Z")

</div>

These instructions produce the following error:  
`[1] pry(main)> User.where(post_count: 0).destroy_all ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column users.post_count does not exist LINE 1: SELECT "users".* FROM "users" WHERE "users"."post_count" = 0 ^ : SELECT "users".* FROM "users" WHERE "users"."post_count" = 0 from /var/www/discourse/vendor/bundle/ruby/2.3.0/gems/rack-mini-profiler-0.10.1/lib/patches/db/pg.rb:90:in `exec'`

This is not really documented I think but if it is please point me in the right direction?

---

<div class="post-metadata">

### Author: ![Mittineague](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mittineague/32/114259_2.png) [@Mittineague](https://meta.discourse.org/u/Mittineague)
#### Post date: [November 28, 2016, 8:32pm UTC](https://meta.discourse.org/t/bulk-remove-users/53450/4 "2016-11-28T20:32:36Z")

</div>

“post count” != “post\_count” ??

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [November 28, 2016, 8:45pm UTC](https://meta.discourse.org/t/bulk-remove-users/53450/5 "2016-11-28T20:45:53Z")

</div>

My bad, the `post_count` property was moved to another table for optimization reasons.

Here’s the updated query

```rails
User.joins(:user_stat).where("user_stats.post_count = 0").destroy_all

```

---

<div class="post-metadata">

### Author: ![yavuz](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/yavuz/32/121388_2.png) [@yavuz](https://meta.discourse.org/u/yavuz)
#### Post date: [November 28, 2016, 9:25pm UTC](https://meta.discourse.org/t/bulk-remove-users/53450/6 "2016-11-28T21:25:01Z")

</div>

Thank you, the updated query works!

---

<div class="post-metadata">

### Author: ![jomaxro](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jomaxro/32/126216_2.png) [@jomaxro](https://meta.discourse.org/u/jomaxro)
#### Post date: [May 21, 2017, 8:13am UTC](https://meta.discourse.org/t/bulk-remove-users/53450/7 "2017-05-21T08:13:34Z")

</div>

One of my sites uses SSO and we’re looking to remove users that have never posted and haven’t been seen in a year (to avoid killing users who are just quiet). Is this query valid in the rails console?

```plaintext
User.joins(:user_stat).where("user_stats.post_count = 0 AND previous_visit_at <= '2016-05-20'::timestamp").destroy_all

```

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [May 21, 2017, 10:15am UTC](https://meta.discourse.org/t/bulk-remove-users/53450/8 "2017-05-21T10:15:35Z")

</div>

It looks good to me.

---

<div class="post-metadata">

### Author: ![Queth](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/queth/32/154753_2.png) [@Queth](https://meta.discourse.org/u/Queth)
#### Post date: [January 29, 2020, 1:03pm UTC](https://meta.discourse.org/t/bulk-remove-users/53450/14 "2020-01-29T13:03:09Z")

</div>

I have the same issue, but with users I need to remove before doing the final import. I have been working on creating a base setup with all settings/themes/whatever before doing the import - now (after a lot of work) I discover I had done this on a partial import that had imported 4000 users already.

I wish to delete those so nobody will accidentally get any emails of digests or whatever (when I forget to change those settings).

How do I adapt that query so only the admin user will remain ?  
There are 4000 or so in the active userlist  
and 30 or so in the suspended users list.

thank you in advance 🙂

---

<div class="post-metadata">

### Author: ![leonardo](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/leonardo/32/228634_2.png) [@leonardo](https://meta.discourse.org/u/leonardo)
#### Post date: [January 13, 2023, 2:27pm UTC](https://meta.discourse.org/t/bulk-remove-users/53450/16 "2023-01-13T14:27:23Z")

</div>

Note: nowadays there is a specific class for destroying Users and associated records cleanly: `UserDestroyer`.

So instead of this:

> [@zogstrip](#):
>
> ```ruby
> User.where(post_count: 0).destroy_all
> 
> ```

This should be done:

```ruby
destroyer = UserDestroyer.new(Discourse.system_user)
User.joins(:user_stat).where("user_stats.post_count = 0").each { |u| destroyer.destroy(u) }

```

---

<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: [January 13, 2023, 2:36pm UTC](https://meta.discourse.org/t/bulk-remove-users/53450/17 "2023-01-13T14:36:47Z")

</div>


