Cant get rid of LDAP Users (and files from them)

I figured, that the better option would be to have all the stuff inside the DB handled from the data within the db itself. So, I wrote a little script that will connect to the database and query oc_preferences for LDAP users, that have marked to be deleted and act on that result set.

The script will start by removing the orphaned home folders inside the NC data folder and then work it’s way back through different tables to remove all existing references to these stale accounts.

The last table to tidy-up is the oc_preferences table, since all sql commands get their work set from that table.

NOTE: this script is provides as-is, not responsibility will be taken for any data loss so use at your own risk! You will also need to adjust the variables at the top with the correct values from your NC’s config.php. And remember… this script will delete data from your NC instance, you probably want to shut down your Apache/Nginx before running this script. Also… this script should be run as the Apache/Nginx user…

#/bin/bash

mysqlUser="db_user"
mysqlPW="db_passwd"
mysqlDB="db_name"
mysqlHost="db_host"
ncData="path to nextcloud data folder"

mysqlCMD="mysql -h $mysqlHost -u $mysqlUser --password=$mysqlPW $mysqlDB -N -e "
# get list if deleted users and remove their resp. homefolders
echo "Now scanning for deleted LDAP user accounts.."
for i in `$mysqlCMD "select userid from oc_preferences where configKey='isDeleted' AND configValue=1;"`; do
        echo "Removing user home: $i"
                rm -rf $ncData/$i
                done

# removing orphaned storages from oc_storages:
echo "Now removing orphaned user storages from oc_storages..."
`$mysqlCMD "delete from oc_storages where id in (select concat('home::',userid) from oc_preferences where configKey='isDeleted' AND configValue=1);"`

#renoving orphaned mounts from oc_mounts:
echo "Now removing orphaned mounts from oc_mounts..."
`$mysqlCMD "delete from oc_storages where id in (select concat('home::',userid) from oc_preferences where configKey='isDeleted' AND configValue=1);"`

#renoving deleted users from oc_ldap_user_mapping:
echo "Now removing deleted users from oc_ldap_user_mapping..."
`$mysqlCMD "delete from oc_ldap_user_mapping where owncloud_name in (select userid from oc_preferences where configKey='isDeleted' AND configValue=1);"`

#renoving deleted users from oc_accounts:
echo "Now removing deleted users from oc_accounts..."
`$mysqlCMD "delete from oc_accounts where uid in (select userid from oc_preferences where configKey='isDeleted' AND configValue=1);"`

# finally remove deleted users from oc_preferences (must be the last action)
 echo "Now removing deleted users from oc_preferences..."
`$mysqlCMD "CREATE TEMPORARY TABLE IF NOT EXISTS oc_preferences_tmp AS (select userid from oc_preferences where configKey='isDeleted' AND configValue=1); delete from oc_preferences where userid in (select userid from oc_preferences_tmp);"`

Also, it would then be a goog idea to have all the home folders to be scanned by NC using:

sudo -u apache php occ files:scan --all