Keep your community vibrant and your user list clean by periodically removing members who haven’t engaged in a long time. This script removes users who have not logged in within a specified number of days.
#!/bin/bash
# Usage: ./delete_inactive_users.sh <days_inactive>
# Check for required arguments
if [ -z "$1" ]; then
echo "Usage: $0 <days_inactive>"
exit 1
fi
DAYS=$1
# Get the current timestamp
NOW=$(date +%s)
# Calculate the timestamp for the cutoff date
CUTOFF=$(date -d "$DAYS days ago" +%s)
echo "Searching for users inactive for more than $DAYS days..."
# Get all user IDs and their last login timestamps
USER_IDS=$(wp user list --field=ID)
for USER_ID in $USER_IDS; do
# Get last login meta. Assumes you have a meta field storing this. BuddyBoss may not by default.
# If you don't have a last login plugin, this script would need modification to use registration date.
LAST_LOGIN_STRING=$(wp user meta get $USER_ID last_login --format=json | jq -r '.[0]')
if [ -z "$LAST_LOGIN_STRING" ] || [ "$LAST_LOGIN_STRING" == "null" ]; then
# Fallback to registration date if last login is not available
REGISTRATION_DATE=$(wp user get $USER_ID --field=user_registered)
LAST_LOGIN=$(date -d "$REGISTRATION_DATE" +%s)
else
LAST_LOGIN=$(date -d "@$LAST_LOGIN_STRING" +%s)
fi
if (( LAST_LOGIN < CUTOFF )); then
USER_INFO=$(wp user get $USER_ID --field=user_login --format=json)
echo "User '$USER_INFO' (ID: $USER_ID) is inactive. Last login: $(date -d @$LAST_LOGIN)"
# Add --reassign=<user_id> to reassign their content to another user
wp user delete $USER_ID --yes
echo "User $USER_ID deleted."
fi
done
echo "Inactive user cleanup complete."