# User cleanup deletion script

## Purpose

This document describes the administrative cleanup script used to remove user records from the LendCore application while preserving certain protected accounts.

The script is intended for the following scenario:

- Delete all non-admin, non-officer users from the database.
- Preserve the protected user with UID 2.
- Preserve users with the `admin` role.
- Preserve users with the `officer` role.
- Remove related loan and transaction data for deleted users through existing database cascade rules.

## Files

- Script: [scripts/delete_specific_user_cleanup.php](../scripts/delete_specific_user_cleanup.php)
- Documentation: [docs/user-cleanup-deletion-script.md](user-cleanup-deletion-script.md)

## What the script does

1. Boots the Laravel application.
2. Finds all users who are not UID 2 and whose `role` is neither `admin` nor `officer`.
3. Deletes those users in a single database transaction.
4. Reports the number of deleted and skipped users.
5. Confirms that UID 2 still exists.

## Safety notes

This is a destructive operation.

- It permanently deletes the selected user records.
- Because the database schema uses cascade delete for user-linked loan and transaction data, the related rows are removed as well.
- The protected admin account is never deleted.
- Run the script only after confirming the target IDs and taking a backup if required.

## Usage

From the project root, run:

```bash
php scripts/delete_specific_user_cleanup.php
```

This version deletes all users who are not UID 2 and whose `role` is neither `admin` nor `officer`.

> Note: The script no longer accepts a target ID list or protected ID override; it operates across the whole user table with the hard-coded exclusions.

## Restarting new user UID assignment

The cleanup script does not change the `users` table auto-increment value. To make the next new user start at the current highest existing UID plus one, run this SQL in MariaDB after cleanup:

```sql
SET @next_uid = (
	SELECT COALESCE(MAX(id), 0) + 1
	FROM users
);

SET @sql = CONCAT(
	'ALTER TABLE users AUTO_INCREMENT = ',
	@next_uid
);

PREPARE statement FROM @sql;
EXECUTE statement;
DEALLOCATE PREPARE statement;
```

This adjusts only the next automatically generated UID. It does not renumber existing users or reuse UID gaps below the current maximum.

## Expected behavior

When the script finds matching users, it will print output similar to:

```text
target_users=3,35,36,37
user_before=3|exists=yes|loans=4|transactions=12
user_before=35|exists=yes|loans=1|transactions=4
user_after=3|exists=no
admin_exists=yes
```

If a requested ID is already absent, the script reports that the user is already missing.

## Related database behavior

The cleanup relies on the existing foreign-key rules defined in the schema:

- Loans linked to a user are deleted when the user is deleted.
- Transactions linked to a user are deleted when the user is deleted.
- Other unrelated data is left unchanged.
