# Bulk deleting / mass modifying users

**URL:** https://meta.discourse.org/t/bulk-deleting-mass-modifying-users/64362
**Category:** Support
**Created:** [June 12, 2017, 3:50pm UTC](https://meta.discourse.org/t/bulk-deleting-mass-modifying-users/64362 "2017-06-12T15:50:17Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![lead4good](https://avatars.discourse-cdn.com/v4/letter/l/e19adc/32.png) [@lead4good](https://meta.discourse.org/u/lead4good)
#### Post date: [June 12, 2017, 3:50pm UTC](https://meta.discourse.org/t/bulk-deleting-mass-modifying-users/64362/1 "2017-06-12T15:50:17Z")

</div>

Recently we’ve been migrating our bbpress forums to discourse at [purism](https://puri.sm/) (a company that builds security focused laptops by trying to free the hardware from binary blobs) . I’ve already wrote a tutorial on [how to import a bbpress backup](https://meta.discourse.org/t/how-to-use-the-bbpress-import-script-or-any-other-import-script-with-mysql-dependency/63349) and now I will focus on another problem we’ve encountered.

After importing the backup we’ve discovered a substantial amount of users who haven’t posted into the forums yet. This was due to the bbpress import script importing all wordpress users. We had to find a way to delete them, and we were sure as hell not going to do that one at a time!

##How to bulk delete users or mass modify them in a specific way?  
In this tutorial I will show how to bulk delete users which have not posted yet.

> **WARNING:** Please be very careful using this script and be sure to [backup your files](https://meta.discourse.org/t/backup-discourse-from-the-command-line/64364) before.

First open a command line on your discourse server and then open the file `delete_users.rb` in your favorite editor. Paste the following contents inside.

```plaintext
require_relative '../config/environment'

pm = 0
User.find_each do |u|
  if u.post_count == 0 && u.topic_count == 0
    puts "Deleting user #{u.id}, #{u.name}, #{u.username}, #{u.email} ..."
    u.destroy
    pm = pm +1
  end
end
puts "Delete a total of #{pm} users"

```

This code loops through all existing users

```plaintext
User.find_each do |u|

```

Finds those who haven’t added a topic or post yet

```plaintext
if u.post_count == 0 && u.topic_count == 0

```

and deletes them

```plaintext
u.destroy

```

You can now copy the script inside your docker container `app`

```console
$ docker cp delete_users.rb app:/var/www/discourse/script/

```

Enter the discourse container `app`

```console
$ ./launcher enter app

```

And then execute the script

```console
$ su discourse -c "bundle exec ruby script/delete_users.rb"

```

If you want to delete your users based on different information or if you want to only modify them I would start searching in the User class at [app/models/user.rb](https://github.com/discourse/discourse/blob/master/app/models/user.rb) in the discourse source code.

---

<div class="post-metadata">

### Author: ![jonathon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jonathon/32/265865_2.png) [@jonathon](https://meta.discourse.org/u/jonathon)
#### Post date: [February 10, 2018, 1:06pm UTC](https://meta.discourse.org/t/bulk-deleting-mass-modifying-users/64362/2 "2018-02-10T13:06:04Z")

</div>

A potentially more efficient way would be to select the users and delete as one transaction:

```ruby
User.joins(:user_stat).where("user_stats.post_count = 0 AND user_stats.topic_count = 0").destroy_all

```

See also:

> [@Bulk delete suspended users after import](https://meta.discourse.org/t/bulk-delete-suspended-users-after-import/73011/3):
>
> For that, you should enter into the container with ./launcher enter app Then, you will need to launch the [rails console](http://guides.rubyonrails.org/command_line.html#rails-console) with rails c Once you’re in the rails console, you can delete all suspended users who have zero posts and topics with User.joins(:user\_stat).where("users.suspended\_at IS NOT NULL AND user\_stats.topic\_count = 0 AND user\_stats.post\_count = 0").destroy\_all Always make sure you have latest db backup whenever you run destroy\_all method.
