# 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:** 4

<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 4, 2024, 8:30pm UTC](https://meta.discourse.org/t/please-eval-short-ruby-script-to-suspend-inactive-users/324795/4 "2024-09-04T20:30:40Z")

</div>

Thanks for your interest, @pfaffman !

> [@TonyG](#):
>
> I also have “purge unactivated users grace period days” set to 7.

> [@pfaffman](#):
>
> Why isn’t that enough to solve your problem?

There are user records with no last\_seen\_at, created months ago, approved=False, active=False, and they are not being purged.  
There are user records with last\_seen\_at \> 7 days (months old) and 0 views, and they are not being purged.

Whatever the criteria being used with that grace period flag, it is not selecting these records.  
Can someone post the exact query that is being used there so that I can understand what other factors are involved there?

> [@TonyG](#):
>
> `User.where("views = 0 OR approved = FALSE OR last_seen_at IS NULL")`

> [@pfaffman](#):
>
> Shouldn’t those be AND rather than OR?

No, looking at the DB directly there are user records that fit each criterion but not all. It seems the users table in the database is inconsistent. There are records with non-zero topics\_entered or posts\_read\_count, but with views=0. There are records with topics\_entered=0 and posts\_read\_count=0, but views is non-zero.

The key point to remember is that as this site was being developed the settings weren’t optimal and humans and bots were registering. The `OR` clause seems to get all of these. Now that the site is stablized with (I hope) sane settings, I don’t expect new registrations to result in the same anomalies.

I intend to run the script many times with different criteria. I will query records first, outside of the environment, and then run the silence script to target only the records I really want. In a couple weeks I’ll do a final run, just select silenced records (anyone who actually came back will get the flag removed), and purge all of them:

`UserDestroyer.new(admin_user).destroy(user, reassign_to: archive_user)`

My overall question is whether the v0.0.2 script, with its approach to selection, logging, and silencing, is correct for a Discourse system. I don’t know if there is something else to be done in a loop like this. I’ve never created and run my own script so this is request to check me for noobish knucklehead mistakes.

Thanks!!

---

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