# Bulk deleting / mass modifying users

**URL:** <https://meta.discourse.org/t/bulk-deleting-mass-modifying-users/64362>\
**Category:** Product Support\
**Created:** [2017 年 6 月 12 日午後 3:50 UTC](https://meta.discourse.org/t/bulk-deleting-mass-modifying-users/64362 "2017-06-12T15:50:17Z")\
**Posts on this page:** 1\
**Showing post:** 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:** [2017 年 6 月 12 日午後 3:50 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.

---

_[View the full topic](https://meta.discourse.org/t/bulk-deleting-mass-modifying-users/64362)._
