mirror of
https://github.com/invoke-ai/InvokeAI
synced 2026-03-02 13:09:06 +01:00
* Add script and UI to remove orphaned model files - This commit adds command-line and Web GUI functionality for identifying and optionally removing models in the models directory that are not referenced in the database. Co-authored-by: lstein <111189+lstein@users.noreply.github.com> * Add backend service and API routes for orphaned models sync Co-authored-by: lstein <111189+lstein@users.noreply.github.com> Add expandable file list to orphaned models dialog Co-authored-by: lstein <111189+lstein@users.noreply.github.com> * Fix cache invalidation after deleting orphaned models Co-authored-by: lstein <111189+lstein@users.noreply.github.com> * (bugfix) improve status messages * docs(backend): add info on the orphaned model detection/removal feature * Update docs/features/orphaned_model_removal.md --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: lstein <111189+lstein@users.noreply.github.com> Co-authored-by: dunkeroni <dunkeroni@gmail.com>
5.4 KiB
5.4 KiB
Orphaned Models Synchronization Feature
Overview
This feature adds a UI for synchronizing the models directory by finding and removing orphaned model files. Orphaned models are directories that contain model files but are not referenced in the InvokeAI database.
Implementation Summary
Backend (Python)
New Service: OrphanedModelsService
- Location:
invokeai/app/services/orphaned_models/ - Implements the core logic from the CLI script
- Methods:
find_orphaned_models(): Scans the models directory and database to find orphaned modelsdelete_orphaned_models(paths): Safely deletes specified orphaned model directories
API Routes
Added to invokeai/app/api/routers/model_manager.py:
GET /api/v2/models/sync/orphaned: Returns list of orphaned models with metadataDELETE /api/v2/models/sync/orphaned: Deletes selected orphaned models
Data Models
OrphanedModelInfo: Contains path, absolute_path, files list, and size_bytesDeleteOrphanedModelsRequest: Contains list of paths to deleteDeleteOrphanedModelsResponse: Contains deleted paths and errors
Frontend (TypeScript/React)
New Components
-
SyncModelsButton.tsx
- Red button styled with
colorScheme="error"for visual prominence - Labeled "Sync Models"
- Opens the SyncModelsDialog when clicked
- Located next to the "+ Add Models" button
- Red button styled with
-
SyncModelsDialog.tsx
- Modal dialog that displays orphaned models
- Features:
- List of orphaned models with checkboxes (default: all checked)
- "Select All" / "Deselect All" toggle
- Shows file count and total size for each model
- "Delete" and "Cancel" buttons
- Loading spinner while fetching data
- Error handling with user-friendly messages
- Automatically shows toast if no orphaned models found
- Shows success/error toasts after deletion
API Integration
- Added
useGetOrphanedModelsQueryanduseDeleteOrphanedModelsMutationhooks toservices/api/endpoints/models.ts - Integrated with RTK Query for efficient data fetching and caching
Translation Strings
Added to public/locales/en.json:
- syncModels, noOrphanedModels, orphanedModelsFound
- orphanedModelsDescription, foundOrphanedModels (with pluralization)
- filesCount, deleteSelected, deselectAll
- Success/error messages for deletion operations
User Experience Flow
- User clicks the red "Sync Models" button in the Model Manager
- System queries the backend for orphaned models
- If no orphaned models:
- Toast message: "The models directory is synchronized. No orphaned files found."
- Dialog closes automatically
- If orphaned models found:
- Dialog shows list with checkboxes (all selected by default)
- User can toggle individual models or use "Select All" / "Deselect All"
- Each model shows:
- Directory path
- File count
- Total size (formatted: B, KB, MB, GB)
- User clicks "Delete {{count}} selected"
- System deletes selected models
- Success/error toasts appear
- Dialog closes
Safety Features
- Database Backup: The service creates a backup before any deletion
- Selective Deletion: Users choose which models to delete
- Path Validation: Ensures paths are within the models directory
- Error Handling: Reports which models failed to delete and why
- Default Selected: All models are selected by default for convenience
- Confirmation Required: User must explicitly click Delete
Technical Details
Directory-Based Detection
The system treats model paths as directories:
- If database has
model-id/file.safetensors, the entiremodel-id/directory belongs to that model - All files and subdirectories within a registered model directory are protected
- Only directories with NO registered models are flagged as orphaned
Supported File Extensions
- .safetensors
- .ckpt
- .pt
- .pth
- .bin
- .onnx
Skipped Directories
- .download_cache
- .convert_cache
- __pycache__
- .git
Testing Recommendations
-
Test with orphaned models:
- Manually copy a model directory to models folder
- Verify it appears in the dialog
- Delete it and verify removal
-
Test with no orphaned models:
- Clean install
- Verify toast message appears
-
Test partial selection:
- Select only some models
- Verify only selected ones are deleted
-
Test error scenarios:
- Invalid paths
- Permission issues
- Verify error messages are clear
Files Changed
Backend
invokeai/app/services/orphaned_models/__init__.py(new)invokeai/app/services/orphaned_models/orphaned_models_service.py(new)invokeai/app/api/routers/model_manager.py(modified)
Frontend
invokeai/frontend/web/src/services/api/endpoints/models.ts(modified)invokeai/frontend/web/src/features/modelManagerV2/subpanels/ModelManager.tsx(modified)invokeai/frontend/web/src/features/modelManagerV2/subpanels/ModelManagerPanel/SyncModelsButton.tsx(new)invokeai/frontend/web/src/features/modelManagerV2/subpanels/ModelManagerPanel/SyncModelsDialog.tsx(new)invokeai/frontend/web/public/locales/en.json(modified)
Future Enhancements
Potential improvements for future versions:
- Show preview of what will be deleted before deletion
- Add option to move orphaned models to archive instead of deleting
- Show disk space that will be freed
- Add filter/search in orphaned models list
- Support for undo operation
- Scheduled automatic cleanup