# "clean up inactive users after days" meaning and statistics

**URL:** <https://meta.discourse.org/t/clean-up-inactive-users-after-days-meaning-and-statistics/335999>\
**Category:** Data & reporting\
**Created:** [November 13, 2024, 5:24pm UTC](https://meta.discourse.org/t/clean-up-inactive-users-after-days-meaning-and-statistics/335999 "2024-11-13T17:24:38Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![Moin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/moin/32/554653_2.png) [@Moin](https://meta.discourse.org/u/Moin)\
**Post date:** [November 13, 2024, 5:43pm UTC](https://meta.discourse.org/t/clean-up-inactive-users-after-days-meaning-and-statistics/335999/2 "2024-11-13T17:43:05Z")

</div>

There are some additional criteria: admins and moderators aren’t deleted  
Also the last seen and the registration date are checked.

> <https://github.com/discourse/discourse/blob/53f9c81790ca232b8dbfa9b04b7f0863cf0d16c2/app/jobs/scheduled/clean_up_inactive_users.rb#L19>

I think something like this should return the number of users that would be deleted

```sql
-- [params]
-- int :active_days_ago

SELECT COUNT(*)
FROM users
WHERE last_posted_at IS NULL
  AND trust_level = 0
  AND admin = false
  AND moderator = false
  AND created_at < CURRENT_DATE - :active_days_ago
  AND (last_seen_at < CURRENT_DATE - :active_days_ago OR last_seen_at IS NULL)
  AND NOT EXISTS (
    SELECT 1 FROM posts WHERE posts.user_id = users.id
  )
  AND NOT EXISTS (
    SELECT 1 FROM topics WHERE topics.user_id = users.id
  )

```

To see which users would be deleted this should work

```sql
-- [params]
-- int :active_days_ago

SELECT 
    id as user_id,
    DATE_PART('day', CURRENT_DATE - created_at) AS days_since_created,
    DATE_PART('day', CURRENT_DATE - last_seen_at) AS days_since_seen
FROM users
WHERE last_posted_at IS NULL
  AND trust_level = 0
  AND admin = false
  AND moderator = false
  AND created_at < CURRENT_DATE - :active_days_ago
  AND (last_seen_at < CURRENT_DATE - :active_days_ago OR last_seen_at IS NULL)
  AND NOT EXISTS (
    SELECT 1 FROM posts WHERE posts.user_id = users.id
  )
  AND NOT EXISTS (
    SELECT 1 FROM topics WHERE topics.user_id = users.id
  )
ORDER BY days_since_seen

```

---

_[View the full topic](https://meta.discourse.org/t/clean-up-inactive-users-after-days-meaning-and-statistics/335999)._
