# Remove all users except admins or a few select users whichever is easier

**URL:** https://meta.discourse.org/t/remove-all-users-except-admins-or-a-few-select-users-whichever-is-easier/249199
**Category:** Support
**Created:** [December 17, 2022, 1:28am UTC](https://meta.discourse.org/t/remove-all-users-except-admins-or-a-few-select-users-whichever-is-easier/249199 "2022-12-17T01:28:43Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![adambirds](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/adambirds/32/131854_2.png) [@adambirds](https://meta.discourse.org/u/adambirds)
#### Post date: [December 17, 2022, 1:28am UTC](https://meta.discourse.org/t/remove-all-users-except-admins-or-a-few-select-users-whichever-is-easier/249199/1 "2022-12-17T01:28:43Z")

</div>

Is there a quick script which will allow me to bulk remove all users except either admins or a select few users identified by their IDs/emails? Whichever option is easier is fine.

Will want to do this from the rails console.

Thanks

---

<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: [December 17, 2022, 3:15am UTC](https://meta.discourse.org/t/remove-all-users-except-admins-or-a-few-select-users-whichever-is-easier/249199/2 "2022-12-17T03:15:15Z")

</div>

```
 User.where(admin: false).destroy_all

```

I think should do it. You can do the above without the destroy\_all to see what users are selected.

---

<div class="post-metadata">

### Author: ![adambirds](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/adambirds/32/131854_2.png) [@adambirds](https://meta.discourse.org/u/adambirds)
#### Post date: [December 17, 2022, 3:19am UTC](https://meta.discourse.org/t/remove-all-users-except-admins-or-a-few-select-users-whichever-is-easier/249199/3 "2022-12-17T03:19:55Z")

</div>

Thanks, is `destroy_all` the same as `UserDestroyer` or is one recommended over the other. Saw UserDestroyer mentioned a few times in other posts. Thanks

---

<div class="post-metadata">

### Author: ![adambirds](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/adambirds/32/131854_2.png) [@adambirds](https://meta.discourse.org/u/adambirds)
#### Post date: [December 17, 2022, 3:55am UTC](https://meta.discourse.org/t/remove-all-users-except-admins-or-a-few-select-users-whichever-is-easier/249199/4 "2022-12-17T03:55:34Z")

</div>

Its now failing with this after deleting around 200, but its now stuck:

```plaintext
ActiveRecord::RecordInvalid: Validation failed: Primary email can't be blank
from /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.3/lib/active_record/validations.rb:80:in `raise_validation_error'

```

I’ve had a search and found one post about checking for blocked emails in Admin \> Logs \> Screened Emails and nothing is showing but not sure what to do next. Thanks

---

<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: [December 17, 2022, 8:25am UTC](https://meta.discourse.org/t/remove-all-users-except-admins-or-a-few-select-users-whichever-is-easier/249199/5 "2022-12-17T08:25:02Z")

</div>

UserDestroyer is what you should use. It will delete other data associated with the user.

Don’t know why you’d get that error.

You might restore your backup to before you started the delete and try the destroyer instead.

---

<div class="post-metadata">

### Author: ![adambirds](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/adambirds/32/131854_2.png) [@adambirds](https://meta.discourse.org/u/adambirds)
#### Post date: [December 17, 2022, 3:00pm UTC](https://meta.discourse.org/t/remove-all-users-except-admins-or-a-few-select-users-whichever-is-easier/249199/6 "2022-12-17T15:00:30Z")

</div>

I’ve restored and tried again with the UserDestroyer, but still getting that error. Anyway to track down whats causing it?

---

<div class="post-metadata">

### Author: ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)
#### Post date: [December 17, 2022, 3:25pm UTC](https://meta.discourse.org/t/remove-all-users-except-admins-or-a-few-select-users-whichever-is-easier/249199/7 "2022-12-17T15:25:57Z")

</div>

You can use a rails script or [Data Explorer](https://meta.discourse.org/t/32566?silent=true) to check for the email addresses and see if one is blank… Though it would be weird. No email field is blank by default even for automatically generated accounts like discobot ou system (which are admins by default).

Were your users imported? But even so, I’m pretty sure that account creation methods don’t allow blank email fields.

---

<div class="post-metadata">

### Author: ![adambirds](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/adambirds/32/131854_2.png) [@adambirds](https://meta.discourse.org/u/adambirds)
#### Post date: [December 17, 2022, 3:31pm UTC](https://meta.discourse.org/t/remove-all-users-except-admins-or-a-few-select-users-whichever-is-easier/249199/8 "2022-12-17T15:31:21Z")

</div>

We’ve used oauth with a custom app. I did this yesterday but returned no users:

`User.joins(:user_emails).where(user_emails.email = "")` and that returned nil so I don’t think any are empty.

Must just be one or two users though because the majority work but its getting stcuk instantly on the same one now.

My delete script is:

```plaintext
User.where(admin: false).find_each do |user| UserDestroyer.new(Discourse.system_user).destroy(user, delete_posts: true) end

```

Is there anyway to echo which user its doing as it loops round?

---

<div class="post-metadata">

### Author: ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)
#### Post date: [December 17, 2022, 3:35pm UTC](https://meta.discourse.org/t/remove-all-users-except-admins-or-a-few-select-users-whichever-is-easier/249199/9 "2022-12-17T15:35:46Z")

</div>

You can output the usernames and their corresponding emails:

```ruby
User.where(admin: false).find_each do |user|
  puts "#{user.username}: #{user.email}"
  UserDestroyer.new(Discourse.system_user).destroy(user, delete_posts: true)
end

```

---

<div class="post-metadata">

### Author: ![adambirds](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/adambirds/32/131854_2.png) [@adambirds](https://meta.discourse.org/u/adambirds)
#### Post date: [December 17, 2022, 3:43pm UTC](https://meta.discourse.org/t/remove-all-users-except-admins-or-a-few-select-users-whichever-is-easier/249199/10 "2022-12-17T15:43:38Z")

</div>

Thanks, this is great, will run this now and hopefully should tell me who it is struggling on.

---

<div class="post-metadata">

### Author: ![adambirds](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/adambirds/32/131854_2.png) [@adambirds](https://meta.discourse.org/u/adambirds)
#### Post date: [December 17, 2022, 5:10pm UTC](https://meta.discourse.org/t/remove-all-users-except-admins-or-a-few-select-users-whichever-is-easier/249199/11 "2022-12-17T17:10:10Z")

</div>

Yeah, this worked, showed me which it was struggling with and it was an illegal character in the email somehow so updated that and it worked. Thanks

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [January 16, 2023, 5:10pm UTC](https://meta.discourse.org/t/remove-all-users-except-admins-or-a-few-select-users-whichever-is-easier/249199/12 "2023-01-16T17:10:28Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
