<p>After deleting the users from the UI they are only soft-deleted. If you look at the user id and find the matching element in your <code>elements</code> table, you will see that this element has a populated <code>dateDeleted</code>.</p>
<p>Run <a href="https://craftcms.com/docs/3.x/gc.html" rel="nofollow noreferrer">garbage collection</a> (<code>./craft gc</code>) to clean up and completely remove everything.</p>

CC BY-SA 4.0
2021-02-09T08:49:43.637Z
2021-02-09T08:49:43.637Z
Oli
<p>You could also use your general config file to set the softDeleteDuration to a very low value, so that soft deleted elements get garbagecollected quickly:</p>
<pre><code>'softDeleteDuration' =&gt; 300, // 5 Mins
</code></pre>
<p>or if you're up to the command line, use</p>
<pre><code>./craft gc --delete-all-trashed
</code></pre>
<p>to immediately hard delete all soft deleted items</p>

CC BY-SA 4.0
2021-02-09T10:59:41.560Z
2021-02-09T10:59:41.560Z
MisterMike
<p>I read a comment from Brandon somewhere that the best way to delete at the database level is to remove rows from <code>craft_elements</code>, which then does the housekeeping around that.</p>
<p>I'm not sure if it is still necessary to run garbage collection as per @Oli's answer above, but I have found that even after doing that rows in <code>craft_usergroups_users</code> persist.</p>
<p>I am therefore doing the following:</p>
<pre><code>DELETE FROM craft_elements
WHERE `type` = 'craft\\elements\\User'
AND `id` NOT IN (/* array of IDs I want to keep*/)
</code></pre>
<p>…and then…</p>
<pre><code>DELETE FROM craft_usergroups_users
WHERE `userId` NOT IN (/* array of IDs I want to keep */)
</code></pre>

CC BY-SA 4.0
2021-04-13T11:09:41.147Z
2021-04-13T11:09:41.147Z
Jonathan Schofield
<p>I'm using Craft 3.6.4.1 and I need to delete a whole tranche of users from the database (that has derived from another Craft instance).</p>
<p>I've been deleting by hand via the CMS but note that loads of user related rows remain in the database:</p>
<ul>
<li>The CMS is reporting only the 4 users which I want to retain, but <code>craft_users</code> still has 10,706 rows in it.</li>
</ul>
<p>From which tables should I clean out rows that pertain to users I want to delete?</p>
<p>I note the additional following tables which seem relevant in ways I'm unsure of:</p>
<pre><code>craft_usergroups_users
craft_userpermissions
craft_userpermissions_usergroups
craft_userpermissions_users
craft_userpreferences
</code></pre>

<users><database>
Deleting user data from the database
# Deleting user data from the database

(Asked by: username_0 on 2021-02-08)

I'm using Craft 3.6.4.1 and I need to delete a whole tranche of users from the database (that has derived from another Craft instance).
I've been deleting by hand via the CMS but note that loads of user related rows remain in the database:

The CMS is reporting only the 4 users which I want to retain, but craft_users still has 10,706 rows in it.

From which tables should I clean out rows that pertain to users I want to delete?
I note the additional following tables which seem relevant in ways I'm unsure of:
craft_usergroups_users
craft_userpermissions
craft_userpermissions_usergroups
craft_userpermissions_users
craft_userpreferences

**Tags:** users, database

---

## 3 answers

### Answer by username_1 on 2021-02-09. Score: 3

After deleting the users from the UI they are only soft-deleted. If you look at the user id and find the matching element in your `elements` table, you will see that this element has a populated `dateDeleted`.

Run [garbage collection](https://craftcms.com/docs/3.x/gc.html) (`./craft gc`) to clean up and completely remove everything.


---

### Answer by username_2 on 2021-02-09. Score: 2

You could also use your general config file to set the softDeleteDuration to a very low value, so that soft deleted elements get garbagecollected quickly:
'softDeleteDuration' => 300, // 5 Mins

or if you're up to the command line, use
./craft gc --delete-all-trashed

to immediately hard delete all soft deleted items


---

### Answer by username_0 on 2021-04-13. Score: 1

I read a comment from Brandon somewhere that the best way to delete at the database level is to remove rows from craft_elements, which then does the housekeeping around that.
I'm not sure if it is still necessary to run garbage collection as per @Oli's answer above, but I have found that even after doing that rows in craft_usergroups_users persist.
I am therefore doing the following:
DELETE FROM craft_elements
WHERE `type` = 'craft\\elements\\User'
AND `id` NOT IN (/* array of IDs I want to keep*/)

…and then…
DELETE FROM craft_usergroups_users
WHERE `userId` NOT IN (/* array of IDs I want to keep */)


---

