# Please eval short Ruby script to suspend inactive users

**URL:** <https://meta.discourse.org/t/please-eval-short-ruby-script-to-suspend-inactive-users/324795>\
**Category:** Development\
**Created:** [September 3, 2024, 11:00pm UTC](https://meta.discourse.org/t/please-eval-short-ruby-script-to-suspend-inactive-users/324795 "2024-09-03T23:00:42Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![TonyG](https://avatars.discourse-cdn.com/v4/letter/t/f14d63/32.png) [@TonyG](https://meta.discourse.org/u/TonyG)\
**Post date:** [September 3, 2024, 11:00pm UTC](https://meta.discourse.org/t/please-eval-short-ruby-script-to-suspend-inactive-users/324795/1 "2024-09-03T23:00:42Z")

</div>

This is my first custom Ruby script ever. 🙂 In almost 50 years of coding with over 20 languages and dialects, I’ve never needed (or wanted 🫢) to write in Ruby, but OK, you got me … please be gentle. 🙏

My goal is to identify user accounts that have not been authenticated by email links, and suspend the accounts so that I can check them before deletion. During initial setup I had settings in various stages of development. The 2FA emails didn’t go out for some registrations, and there were a lot of bogus registrations that I’d now like to filter out.

> **Version 0.0.1**
>
> `rails c`
> 
> ```plaintext
> # First ensure user TLs conform to latest definitions
> User.all.find_each do |user|
> Promotion.recalculate(user)
> end
> Group.ensure_consistency!
> 
> # Prep for loop
> logger = StaffActionLogger.new(User.where("id = 'admin'"))
> # temporary date, users will be removed before this date
> suspend_till = DateTime.new(2028, 12, 31)
> suspend_at = DateTime.now
> reason = 'Inactive'
> 
> # Identify potentially dead accounts
> User.where("views = 0 OR approved = FALSE OR last_seen_at IS NULL")
> .where("id > 0")
> .find_each do |user|
> user.suspended_till = suspend_till
> user.suspended_at = suspend_at
> user.change_trust_level!(TrustLevel[0])
> # save the user, log the action, and ... do whatever is done for this trigger
> user.save!
> logger.log_user_suspend(user, reason)
> DiscourseEvent.trigger(:user_suspended, user: user)
> # avoid abusing the mail server
> sleep(10)
> end
> 
> ```

By triggering the event, I’d like emails to go out to the users - there will be a bunch of bounces, and there might be a few people who come back to authenticate. After a week I’ll select all accounts that are still suspended and delete them.

Questions:

1. In general, will this do what is intended?
2. Is there a better approach?
3. Is the logging redundant with the event trigger?
4. Can we do this with JavaScript?
5. Should I post this to another category?
6. Any other words of advice?

Thank you!!

---

_[View the full topic](https://meta.discourse.org/t/please-eval-short-ruby-script-to-suspend-inactive-users/324795)._
