# Database Management Commands

This document provides information about custom database management commands available in the application.

## Clear Database Except Users

This command allows you to clear all data from the database while preserving user accounts and their passwords. It's useful for resetting the application to a clean state without having to recreate user accounts.

### Command

```bash
php artisan app:clear-except-users
```

### What it does

- Clears all data from all tables except:
  - `users` table (preserves all user accounts and their passwords)
  - Laravel system tables (`migrations`, `password_reset_tokens`, `password_resets`, `failed_jobs`, `personal_access_tokens`)
- Disables foreign key checks during the operation to avoid constraint errors
- Re-enables foreign key checks after completion
- Provides detailed output of which tables are being cleared and which are being skipped

### When to use

- When you want to reset the application data to a clean state
- When you need to clear test data but keep user accounts
- Before importing new data sets
- When troubleshooting data-related issues

### Example output

```
Starting to clear database tables...
Clearing table: chart_of_accounts
Clearing table: clients
Clearing table: credit_note_items
Clearing table: credit_notes
Clearing table: delivery_note_items
Clearing table: delivery_notes
Clearing table: events
Clearing table: expenses
Skipping table: failed_jobs
Clearing table: financial_settings
Clearing table: fiscal_periods
Clearing table: general_ledger
Clearing table: inventory_categories
Clearing table: inventory_items
Clearing table: invoices
Clearing table: journal_entries
Clearing table: journal_entry_details
Skipping table: migrations
Skipping table: password_reset_tokens
Skipping table: password_resets
Clearing table: payment_methods
Skipping table: personal_access_tokens
Clearing table: proforma_invoice_items
Clearing table: proforma_invoices
Clearing table: quotation_items
Clearing table: quotations
Clearing table: receipts
Clearing table: return_note_items
Clearing table: return_notes
Clearing table: staff
Clearing table: staff_assignments
Clearing table: staff_roles
Skipping table: users
Database cleared successfully. 27 tables were cleared.
Users and their passwords have been preserved.
```

### Notes

- This command uses the `TRUNCATE` SQL statement, which is faster than `DELETE` but cannot be rolled back
- Foreign key checks are temporarily disabled to allow truncating tables with foreign key constraints
- The command is safe to use in production, but should be used with caution as it permanently deletes data
