Database Cleanup Reference Guide
Strategic Intelligence Hub D1 Database
This document provides the correct order for cleaning up data in the strategic-intelligence-hub D1 database while respecting foreign key constraints.
Database Schema Overview
Core Application Tables (DO NOT DELETE)
These tables contain the application structure and should never be cleaned:
industries- Industry definitions and configurationsonboarding_flows- Workflow templatesflow_steps- Step definitions within workflowsstep_questions- Question templates for each stepuser_terms_acceptance- Legal compliance tracking
Client Data Tables (SAFE TO CLEAN)
These tables contain user-submitted data and can be cleaned:
client_profiles- User company and contact informationorchestrator_submissions- Complete profile submissions to processing systemclient_responses- Individual question responsesclient_objectives- Client business objectivesclient_compliance- Client compliance requirementsreport_assets- Generated reports and documentsorchestrator_processing_log- Processing execution logs
Foreign Key Relationships
Dependency Chain (Child → Parent)
report_assets → orchestrator_submissions (via tracking_id)
orchestrator_processing_log → orchestrator_submissions (via tracking_id)
orchestrator_submissions → client_profiles (via client_id)
client_responses → client_profiles (via client_id)
client_objectives → client_profiles (via client_id)
client_compliance → client_profiles (via client_id)Visual Dependency Tree
client_profiles (ROOT)
├── orchestrator_submissions
│ ├── report_assets
│ └── orchestrator_processing_log
├── client_responses
├── client_objectives
└── client_complianceCleanup Procedures
Option 1: Remove Test Data Only (Safest)
Step 1: Preview What Will Be Deleted
sql
-- Preview test records before deletion
SELECT
cp.id,
cp.company_name,
cp.email,
cp.created_at,
'TEST RECORD' as reason
FROM client_profiles cp
WHERE
LOWER(cp.company_name) LIKE '%test%'
OR LOWER(cp.company_name) LIKE '%deployment%'
OR LOWER(cp.company_name) LIKE '%decoupling%'
OR LOWER(cp.email) LIKE '%test%'
OR LOWER(cp.email) LIKE '%deployment%'
ORDER BY cp.created_at DESC;Step 2: Execute Cleanup (Correct Order)
bash
# 1. Delete child records first
wrangler d1 execute strategic-intelligence-hub --remote --command="DELETE FROM report_assets WHERE tracking_id IN (SELECT tracking_id FROM orchestrator_submissions WHERE client_id IN (SELECT id FROM client_profiles WHERE LOWER(company_name) LIKE '%test%' OR LOWER(email) LIKE '%test%'));"
# 2. Delete processing logs
wrangler d1 execute strategic-intelligence-hub --remote --command="DELETE FROM orchestrator_processing_log WHERE tracking_id IN (SELECT tracking_id FROM orchestrator_submissions WHERE client_id IN (SELECT id FROM client_profiles WHERE LOWER(company_name) LIKE '%test%' OR LOWER(email) LIKE '%test%'));"
# 3. Delete submissions
wrangler d1 execute strategic-intelligence-hub --remote --command="DELETE FROM orchestrator_submissions WHERE client_id IN (SELECT id FROM client_profiles WHERE LOWER(company_name) LIKE '%test%' OR LOWER(email) LIKE '%test%');"
# 4. Delete other client data
wrangler d1 execute strategic-intelligence-hub --remote --command="DELETE FROM client_responses WHERE client_id IN (SELECT id FROM client_profiles WHERE LOWER(company_name) LIKE '%test%' OR LOWER(email) LIKE '%test%');"
wrangler d1 execute strategic-intelligence-hub --remote --command="DELETE FROM client_objectives WHERE client_id IN (SELECT id FROM client_profiles WHERE LOWER(company_name) LIKE '%test%' OR LOWER(email) LIKE '%test%');"
wrangler d1 execute strategic-intelligence-hub --remote --command="DELETE FROM client_compliance WHERE client_id IN (SELECT id FROM client_profiles WHERE LOWER(company_name) LIKE '%test%' OR LOWER(email) LIKE '%test%');"
# 5. Finally delete parent records
wrangler d1 execute strategic-intelligence-hub --remote --command="DELETE FROM client_profiles WHERE LOWER(company_name) LIKE '%test%' OR LOWER(email) LIKE '%test%';"Option 2: Complete Client Data Reset (Nuclear)
Step 1: Create Backup
bash
wrangler d1 export strategic-intelligence-hub --output=db-backup-$(date +%Y%m%d-%H%M%S).sql --remoteStep 2: Execute Complete Cleanup (Correct Order)
bash
# Delete in dependency order (children first, parents last)
wrangler d1 execute strategic-intelligence-hub --remote --command="DELETE FROM report_assets;"
wrangler d1 execute strategic-intelligence-hub --remote --command="DELETE FROM orchestrator_processing_log;"
wrangler d1 execute strategic-intelligence-hub --remote --command="DELETE FROM orchestrator_submissions;"
wrangler d1 execute strategic-intelligence-hub --remote --command="DELETE FROM client_responses;"
wrangler d1 execute strategic-intelligence-hub --remote --command="DELETE FROM client_objectives;"
wrangler d1 execute strategic-intelligence-hub --remote --command="DELETE FROM client_compliance;"
wrangler d1 execute strategic-intelligence-hub --remote --command="DELETE FROM client_profiles;"Option 3: Date-Based Cleanup
Remove Records Older Than X Days
bash
# Example: Remove records older than 30 days
wrangler d1 execute strategic-intelligence-hub --remote --command="DELETE FROM report_assets WHERE tracking_id IN (SELECT tracking_id FROM orchestrator_submissions WHERE DATE(submitted_at) < DATE('now', '-30 days'));"
wrangler d1 execute strategic-intelligence-hub --remote --command="DELETE FROM orchestrator_processing_log WHERE tracking_id IN (SELECT tracking_id FROM orchestrator_submissions WHERE DATE(submitted_at) < DATE('now', '-30 days'));"
wrangler d1 execute strategic-intelligence-hub --remote --command="DELETE FROM orchestrator_submissions WHERE DATE(submitted_at) < DATE('now', '-30 days');"
wrangler d1 execute strategic-intelligence-hub --remote --command="DELETE FROM client_responses WHERE client_id IN (SELECT id FROM client_profiles WHERE DATE(created_at) < DATE('now', '-30 days'));"
wrangler d1 execute strategic-intelligence-hub --remote --command="DELETE FROM client_objectives WHERE client_id IN (SELECT id FROM client_profiles WHERE DATE(created_at) < DATE('now', '-30 days'));"
wrangler d1 execute strategic-intelligence-hub --remote --command="DELETE FROM client_compliance WHERE client_id IN (SELECT id FROM client_profiles WHERE DATE(created_at) < DATE('now', '-30 days'));"
wrangler d1 execute strategic-intelligence-hub --remote --command="DELETE FROM client_profiles WHERE DATE(created_at) < DATE('now', '-30 days');"Verification Commands
Check Record Counts
sql
SELECT
(SELECT COUNT(*) FROM client_profiles) as client_profiles_count,
(SELECT COUNT(*) FROM orchestrator_submissions) as submissions_count,
(SELECT COUNT(*) FROM client_responses) as responses_count,
(SELECT COUNT(*) FROM client_objectives) as objectives_count,
(SELECT COUNT(*) FROM client_compliance) as compliance_count,
(SELECT COUNT(*) FROM report_assets) as assets_count,
(SELECT COUNT(*) FROM orchestrator_processing_log) as processing_log_count;Check Database Size
sql
SELECT
name,
(SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name=sqlite_master.name) as table_exists,
CASE
WHEN name IN ('client_profiles', 'orchestrator_submissions', 'client_responses', 'client_objectives', 'client_compliance', 'report_assets', 'orchestrator_processing_log')
THEN 'CLIENT_DATA'
ELSE 'SYSTEM_DATA'
END as data_type
FROM sqlite_master
WHERE type='table'
ORDER BY data_type, name;List Recent Records
sql
-- Check most recent submissions (should be empty after cleanup)
SELECT id, company_name, email, created_at
FROM client_profiles
ORDER BY created_at DESC
LIMIT 10;Critical Rules
DO NOT DELETE THESE TABLES
industriesonboarding_flowsflow_stepsstep_questionsuser_terms_acceptanced1_migrationssqlite_sequence_cf_METADATA_cf_KV
ALWAYS DELETE IN THIS ORDER
report_assets(references orchestrator_submissions)orchestrator_processing_log(references orchestrator_submissions)orchestrator_submissions(references client_profiles)client_responses(references client_profiles)client_objectives(references client_profiles)client_compliance(references client_profiles)client_profiles(root table)
NEVER
- Delete system/configuration tables
- Delete in reverse order (will cause FK constraint errors)
- Skip the backup step for complete resets
- Delete without verifying results
Emergency Recovery
If You Accidentally Delete System Tables
bash
# Stop immediately and restore from backup
wrangler d1 import strategic-intelligence-hub --file=db-backup-YYYYMMDD-HHMMSS.sql --remoteIf Foreign Key Errors Occur
The error usually means you're trying to delete a parent before its children. Follow the correct order above.
Quick Commands Reference
bash
# List all databases
wrangler d1 list
# Create backup
wrangler d1 export strategic-intelligence-hub --output=backup.sql --remote
# Execute SQL command
wrangler d1 execute strategic-intelligence-hub --remote --command="SQL_HERE"
# Import from backup
wrangler d1 import strategic-intelligence-hub --file=backup.sql --remote
# Check table structure
wrangler d1 execute strategic-intelligence-hub --remote --command="SELECT name FROM sqlite_master WHERE type='table';"Last Updated: August 31, 2025
Database: strategic-intelligence-hub
Environment: Production (--remote flag required)