# Complete Database Reset Guide This guide explains how to force a complete reset of the database so that the cache/data is built completely from scratch. ## Overview There are three main approaches to resetting the database: 1. **API-based reset** (Recommended) - Clears cache via API and triggers rebuild 2. **Database volume reset** - Completely removes PostgreSQL volume and recreates 3. **Manual SQL reset** - Direct database commands to clear all data ## Option 1: API-Based Reset (Recommended) This is the cleanest approach as it uses the application's built-in endpoints. ### Using the Script ```bash # Run the automated reset script ./scripts/reset-and-rebuild.sh ``` The script will: 1. Clear all cached data via `DELETE /api/cache/clear` 2. Trigger a full sync via `POST /api/cache/sync` 3. Monitor the process ### Manual API Calls If you prefer to do it manually: ```bash # Set your backend URL BACKEND_URL="http://localhost:3001" API_URL="$BACKEND_URL/api" # Step 1: Clear all cache curl -X DELETE "$API_URL/cache/clear" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer YOUR_TOKEN" # Step 2: Trigger full sync curl -X POST "$API_URL/cache/sync" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer YOUR_TOKEN" ``` **Note:** You need to be authenticated. Either: - Use a valid JWT token from your session - Or authenticate via the UI first and copy the token from browser dev tools ### Via the UI 1. Navigate to **Settings → Cache Management** 2. Click **"Clear All Cache"** button 3. Click **"Full Sync"** button 4. Monitor progress in the same page ## Option 2: Database Volume Reset (Nuclear Option) This completely removes the PostgreSQL database and recreates it from scratch. ### Using the Reset Script ```bash # Run the PostgreSQL reset script ./scripts/reset-postgres.sh ``` This will: 1. Stop containers 2. Remove the PostgreSQL volume (deletes ALL data) 3. Restart PostgreSQL with a fresh database 4. Wait for PostgreSQL to be ready **⚠️ Warning:** This deletes ALL data including: - All cached CMDB objects - All relations - All attribute values - Schema discovery cache - Sync metadata ### Manual Volume Reset ```bash # Step 1: Stop containers docker-compose down # Step 2: Remove PostgreSQL volume docker volume ls | grep postgres docker volume rm cmdb-insight_postgres_data # Step 3: Start PostgreSQL again docker-compose up -d postgres # Step 4: Wait for PostgreSQL to be ready docker-compose exec postgres pg_isready -U cmdb ``` ### After Volume Reset After resetting the volume, you need to: 1. **Start the backend** - The schema will be created automatically: ```bash docker-compose up -d backend ``` 2. **Check logs** to verify schema creation: ```bash docker-compose logs -f backend ``` You should see: - `NormalizedCacheStore: Database schema initialized` - `SchemaDiscovery: Schema discovery complete` 3. **Trigger a full sync** to rebuild data: - Via UI: Settings → Cache Management → Full Sync - Via API: `POST /api/cache/sync` - Or use the reset script: `./scripts/reset-and-rebuild.sh` ## Option 3: Manual SQL Reset If you want to clear data but keep the database structure: ### For PostgreSQL ```bash # Connect to database docker-compose exec postgres psql -U cmdb -d cmdb_insight # Clear all data (keeps schema) TRUNCATE TABLE attribute_values CASCADE; TRUNCATE TABLE object_relations CASCADE; TRUNCATE TABLE objects CASCADE; TRUNCATE TABLE sync_metadata CASCADE; TRUNCATE TABLE schema_cache CASCADE; TRUNCATE TABLE schema_mappings CASCADE; # Exit \q ``` Then trigger a full sync: ```bash curl -X POST "http://localhost:3001/api/cache/sync" \ -H "Authorization: Bearer YOUR_TOKEN" ``` ### For SQLite (if using SQLite) ```bash # Connect to backend container docker-compose exec backend sh # Clear all data sqlite3 /app/data/cmdb-cache.db < console.log('OK')).catch(e => console.error(e)); " ``` ## Complete Reset Workflow For a complete "green field" reset: ```bash # 1. Stop everything docker-compose down # 2. Remove PostgreSQL volume (nuclear option) docker volume rm cmdb-insight_postgres_data # 3. Start PostgreSQL docker-compose up -d postgres # 4. Wait for PostgreSQL sleep 5 docker-compose exec postgres pg_isready -U cmdb # 5. Start backend (creates schema automatically) docker-compose up -d backend # 6. Wait for backend to initialize sleep 10 docker-compose logs backend | grep "initialization complete" # 7. Clear cache and trigger sync (via script) ./scripts/reset-and-rebuild.sh # OR manually via API curl -X DELETE "http://localhost:3001/api/cache/clear" \ -H "Authorization: Bearer YOUR_TOKEN" curl -X POST "http://localhost:3001/api/cache/sync" \ -H "Authorization: Bearer YOUR_TOKEN" ``` ## Quick Reference | Method | Speed | Data Loss | Schema Loss | Recommended For | |--------|-------|-----------|-------------|-----------------| | API Clear + Sync | Fast | Cache only | No | Regular resets | | Volume Reset | Medium | Everything | Yes | Complete rebuild | | SQL TRUNCATE | Fast | Cache only | No | Quick clear | ## Related Documentation - [Local PostgreSQL Reset](./LOCAL-POSTGRES-RESET.md) - Detailed PostgreSQL reset guide - [Local Development Setup](./LOCAL-DEVELOPMENT-SETUP.md) - Initial setup guide - [Database Schema](./DATABASE-DRIVEN-SCHEMA-IMPLEMENTATION-PLAN.md) - Schema documentation