Bulk deactivation of inactive users

AI-generated summary

Bulk Deactivation of Inactive Users

The discussion revolves around deactivating users who haven’t visited the forum in a long time to prevent unwanted emails. codechef initially asked if there’s a way to bulk deactivate users with a ‘last seen’ date prior to a specific date.

pfaffman suggested an alternative solution by adjusting the suppress digest email after days setting. However, this only impacts digest emails, and codechef pointed out that many categories are set to “watch” by default, resulting in per-post emails.

pfaffman provided a script to bulk deactivate users based on the ‘last seen’ date, which codechef confirmed worked perfectly after testing in a demo environment. codechef also suggested minor improvements to the script.

As for whether bulk deactivation is now a feature in Discourse, agemo asked if it’s still necessary to run the script. JammyDodger responded by mentioning a plugin, discourse-auto-deactivate, which may be useful for this purpose.

I migrated about 250 users that I’d now like to mark as inactive because they haven’t visited the forum in quite some time. Is there a way to bulk deactivate users with a ‘last seen’ date prior to some date?

My goal is to prevent emails from sending to these users. I think deactivation is the correct status because it would allow those users to re-activate if they want - correct?

A simpler solution might be to adjust suppress digest email after days. They’d still be “active” but they’d stop getting notices.

But, if you want, it’s something like this:

cd /var/discourse
./launcher enter app
rails c
User.where("last_seen_at < '2016-01-01'").update_all(active: false)
exit
exit

That setting seems to only impact digest emails. Many of the categories are setup as “watch” by default, so users are getting per-post emails - not digest. Do I understand that correctly?

EDIT: I see emails going out to users that have been inactive for more than 365 days.

I think you’re right. The code I provided, though untested, should do what you want. You should backup your database before trying it.

Quick follow up. This script worked perfectly! I tested it in a demo environment first.

Two minor suggestions:

  1. I added “and active = true” to the where criteria because I wasn’t sure if there’d be side effects to deactivating an inactive user.
  2. People that come across this post is the future may want to consider adding “and id > 0” to prevent updating the system and discobot users (though I didn’t do this myself because they were within my last_seen_at window.) It doesn’t appear that their last_seen_at is updated after the accounts are created.

Since this is an old topic is bulk deactivation of inactive users now a feature of discourse or do you still need to run @pfaffman code as the only solution?

I think there’s a plugin now which you may find useful:

https://github.com/discourse/discourse-auto-deactivate