# How to programmatically log out lots of users

**URL:** https://meta.discourse.org/t/how-to-programmatically-log-out-lots-of-users/37611
**Category:** Support
**Created:** [1월 7, 2016, 9:16오후 UTC](https://meta.discourse.org/t/how-to-programmatically-log-out-lots-of-users/37611 "2016-01-07T21:16:47Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![eriko](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eriko/32/1915_2.png) [@eriko](https://meta.discourse.org/u/eriko)
#### Post date: [1월 7, 2016, 9:16오후 UTC](https://meta.discourse.org/t/how-to-programmatically-log-out-lots-of-users/37611/1 "2016-01-07T21:16:47Z")

</div>

So I need to end a bunch of people login sessions at once on a quarterly basis. I notice that admin/users.rb log\_out uses `MessageBus.publish "/logout", @user.id, user_ids: [@user.id]`. Can I use that inside a plugin to mass logout users? I have ‘log out strict’ set so it should hit all the sessions.

---

<div class="post-metadata">

### Author: ![Mittineague](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mittineague/32/114259_2.png) [@Mittineague](https://meta.discourse.org/u/Mittineague)
#### Post date: [1월 9, 2016, 2:55오전 UTC](https://meta.discourse.org/t/how-to-programmatically-log-out-lots-of-users/37611/10 "2016-01-09T02:55:32Z")

</div>

Sorry if I’m missing the obvious, but wouldn’t putting the forum into “read only mode” be effectively the same as logging everyone off?

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [1월 9, 2016, 1:48오후 UTC](https://meta.discourse.org/t/how-to-programmatically-log-out-lots-of-users/37611/11 "2016-01-09T13:48:12Z")

</div>

Nope. Read only mode will prevent login (for anon) and posting (for logged in users) but once disabled it will not force users to log back in.

---

<div class="post-metadata">

### Author: ![fefrei](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fefrei/32/119538_2.png) [@fefrei](https://meta.discourse.org/u/fefrei)
#### Post date: [2월 3, 2016, 3:32오후 UTC](https://meta.discourse.org/t/how-to-programmatically-log-out-lots-of-users/37611/15 "2016-02-03T15:32:47Z")

</div>

As an alternative, here is a bit of JavaScript for node.js that iterates over a list of user ids and logs them out using the API:

```plaintext
var logoutUsers = function (ids, domain, apiKey, delay) {
    var nextIndex = 0;
    var errors = 0;

    var callNextUrl = function () {
        if (nextIndex < ids.length) {
            var id = ids[nextIndex];
            nextIndex++;
            console.log("logging out " + id);
            var req = https.request({
                hostname: domain,
                port: 443,
                path: '/admin/users/' + id + '/log_out?api_key=' + apiKey + '&api_username=system',
                method: 'POST'
            }, function (res) {
                console.log("response: " + res.statusCode);
                res.resume();
                setTimeout(callNextUrl, delay);
            }).on('error', function (e) {
                console.log("Got error: " + e.message);
                errors++;
            });
            req.end();
        } else {
            console.log("Done!");
            console.log("Errors: " + errors);
        }
    };

    callNextUrl();
};

```

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [12월 17, 2018, 3:56오전 UTC](https://meta.discourse.org/t/how-to-programmatically-log-out-lots-of-users/37611/18 "2018-12-17T03:56:08Z")

</div>

Current best practice is:

```plaintext
./launcher enter app
rails c
UserAuthToken.delete_all

```

Also … for extra safety you could also delete all user api keys used for the app with

```plaintext
UserApiKey.delete_all

```

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [1월 16, 2019, 3:56오전 UTC](https://meta.discourse.org/t/how-to-programmatically-log-out-lots-of-users/37611/19 "2019-01-16T03:56:54Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
