Skip to content

Instantly share code, notes, and snippets.

@adeel-raza
Last active August 8, 2024 10:14
Show Gist options
  • Save adeel-raza/45530060dc9b0deb2d0c65d850ffb5c8 to your computer and use it in GitHub Desktop.
Save adeel-raza/45530060dc9b0deb2d0c65d850ffb5c8 to your computer and use it in GitHub Desktop.
Delete all users on a WordPress site who haven't logged in for one year
-- Step 1: Delete from wp_usermeta
DELETE FROM wp_usermeta
WHERE user_id IN (
SELECT ID
FROM wp_users
WHERE ID IN (
SELECT user_id
FROM wp_usermeta
WHERE meta_key = 'last_login'
AND FROM_UNIXTIME(meta_value) < DATE_SUB(NOW(), INTERVAL 1 YEAR)
)
);
-- Step 2: Delete from wp_users
DELETE FROM wp_users
WHERE ID IN (
SELECT user_id
FROM wp_usermeta
WHERE meta_key = 'last_login'
AND FROM_UNIXTIME(meta_value) < DATE_SUB(NOW(), INTERVAL 1 YEAR)
);
@adeel-raza
Copy link
Author

adeel-raza commented Aug 8, 2024

First try it on a staging site dont directly use on production. You can run this MySQL query from your PHPMyadmin area in your database management on the hosting. This should remove the ones who haven't logged in for a year via the INTERVAL 1 YEAR Command in the query yiu can adjust it according to your requirement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment