# Bulk suspend users based on criteria

**URL:** <https://meta.discourse.org/t/bulk-suspend-users-based-on-criteria/76804>\
**Category:** Product Support\
**Created:** [27.Декабрь.2017 08:06:27 UTC](https://meta.discourse.org/t/bulk-suspend-users-based-on-criteria/76804 "2017-12-27T08:06:27Z")\
**Posts on this page:** 1\
**Showing post:** 18

<div class="post-metadata">

**Author:** ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)\
**Post date:** [07.Июнь.2019 23:44:21 UTC](https://meta.discourse.org/t/bulk-suspend-users-based-on-criteria/76804/18 "2019-06-07T23:44:21Z")

</div>

> [@omarfilip](#):
>
> I would like to include a reason for the suspension, but the users schema doesn’t list it, so what to use?

I spent some time figuring out how to do this. The easiest approach I found was to use the `StaffActionLogger` class to add an entry to the `UserHistory` Table. To use the `StaffActionLogger` class you need to initialize an object with an admin user from your site. For example, on my site, my user ID is `1`, so I initialize the object and assign it to a variable with:

```ruby
logger = StaffActionLogger.new(User.find(1)) # use your user ID instead of 1 here.

```

With an initialized `StaffActionLogger`, you can then use [Blake’s code](https://meta.discourse.org/t/bulk-suspend-users-based-on-criteria/76804/4), but add a suspension reason and a line to enter a record into the `UserHistory` table:

```ruby
suspend_till = DateTime.new(2057,1,1)
reason = 'Your Suspension Reason'
users = User.where(last_seen_at: nil, id: 1..Float::INFINITY, admin: false)
users.each do |u|
  u.suspended_till = suspend_till
  u.suspended_at = DateTime.now
  u.save!

  logger.log_user_suspend(u,reason)
end

```

The suspension reason will be displayed on the user’s card and profile page. Suspended users will see the reason if they attempt to login to the site. This will also add an entry to your Staff Action logs for each suspended user.

One thing to note, if you are running the above code in the rails console, you’ll need to initialize the `logger` variable before copying the rest of the code into the console

---

_[View the full topic](https://meta.discourse.org/t/bulk-suspend-users-based-on-criteria/76804)._
