# מחק את כל המשתמשים שאינם בקבוצה ספציפית

**URL:** <https://meta.discourse.org/t/delete-all-users-not-in-a-specific-group/198804>\
**Category:** Product Support\
**Created:** [1 באוגוסט,‏ 2021,‏ 9:55am UTC](https://meta.discourse.org/t/delete-all-users-not-in-a-specific-group/198804 "2021-08-01T09:55:09Z")\
**Posts on this page:** 1\
**Showing post:** 1

<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:** [1 באוגוסט,‏ 2021,‏ 9:55am UTC](https://meta.discourse.org/t/delete-all-users-not-in-a-specific-group/198804/1 "2021-08-01T09:55:09Z")

</div>

As part of a Discourse to Discourse migration / merger I’m trying to delete all of the users that are not in a specific group.

I tried this in the rails console:  
`User.joins(:group_users).where.not(group_users:{group_id:135}).destroy_all`

Unfortunately, this deleted all users, which was a bit of a pain!

I have found a cumbersome way to do it using the UI and some ugly rails code, but is there an easy way to do this? I’ve probably just stuffed up the syntax.

Here is what I’ve tried, after painfully making a mega group of those to be deleted (about 6000) via the GUI:

```plaintext
rails c
 target_group = Group.find_by_name("nz-not")
 users = User.joins(:group_users).where(group_users:{group_id: target_group.id})
 users.each do |u|
 u.destroy
 end
exit

```

Also, what is the difference between using `destroy` and `UserDestroyer`? Should I be using the latter?

---

_[View the full topic](https://meta.discourse.org/t/delete-all-users-not-in-a-specific-group/198804)._
