-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprune_cli_interactive.py
More file actions
923 lines (796 loc) · 38.9 KB
/
prune_cli_interactive.py
File metadata and controls
923 lines (796 loc) · 38.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
#!/usr/bin/env python3
"""
Open WebUI Interactive Prune CLI
This is an interactive command-line interface for the Open WebUI prune operations,
featuring a beautiful UI with menus, confirmations, and visual feedback.
Requires: rich library for terminal UI
pip install rich
"""
import sys
import os
import logging
from pathlib import Path
from typing import Optional
# Setup path to import modules
SCRIPT_DIR = Path(__file__).resolve().parent
REPO_ROOT = SCRIPT_DIR.parent
sys.path.insert(0, str(REPO_ROOT))
try:
from rich.console import Console
from rich.prompt import Prompt, Confirm, IntPrompt
from rich.table import Table
from rich.panel import Panel
from rich.progress import Progress, SpinnerColumn, TextColumn
from rich.markdown import Markdown
from rich import box
from rich.tree import Tree
from rich.syntax import Syntax
except ImportError:
print("ERROR: This script requires the 'rich' library for terminal UI")
print("Install it with: pip install rich")
sys.exit(1)
try:
from prune_models import PruneDataForm, PrunePreviewResult
from prune_core import PruneLock, get_vector_database_cleaner, ChromaDatabaseCleaner, PGVectorDatabaseCleaner
from prune_operations import (
count_inactive_users,
count_old_chats,
count_orphaned_records,
count_orphaned_uploads,
count_audio_cache_files,
get_active_file_ids,
get_all_folders,
safe_delete_file_by_id,
cleanup_orphaned_uploads,
delete_inactive_users,
cleanup_audio_cache,
delete_orphaned_chat_messages,
)
# Import Open WebUI modules using compatibility layer (handles pip/docker/git installs)
from prune_imports import (
Users, Chats, Files, Notes, Prompts, Models, Knowledges, Functions,
Tools, Skills, Folders, get_db, CACHE_DIR, VECTOR_DB_CLIENT, VECTOR_DB
)
import time
import sqlite3
from sqlalchemy import text
except ImportError as e:
print(f"ERROR: Failed to import required modules: {e}")
print("\nMake sure:")
print(" 1. You're running from the Open WebUI directory")
print(" 2. Open WebUI dependencies are installed")
print(" 3. For git install: backend/requirements.txt must be installed")
sys.exit(1)
console = Console()
log = logging.getLogger(__name__)
class InteractivePruneUI:
"""Interactive UI for prune operations."""
def __init__(self):
self.form_data = PruneDataForm()
self.vector_cleaner = None
def _get_all_folders_safe(self, db=None):
"""
Safely get all folders using compatibility helper.
Handles API changes between Open WebUI versions.
Args:
db: Optional database session to reuse
"""
return get_all_folders(db=db)
def run(self):
"""Main entry point for interactive UI."""
self.show_welcome()
# Check environment
if not self.check_environment():
return 1
# Initialize prune lock
PruneLock.init(Path(CACHE_DIR))
# Main menu loop
while True:
action = self.show_main_menu()
if action == "configure":
self.configure_settings()
elif action == "preview":
self.run_preview()
elif action == "execute":
if self.confirm_execution():
self.run_execution()
elif action == "help":
self.show_help()
elif action == "exit":
console.print("\n[yellow]Goodbye![/yellow]")
return 0
def show_welcome(self):
"""Show welcome message."""
console.clear()
console.print(Panel.fit(
"[bold cyan]Open WebUI Interactive Prune Tool[/bold cyan]\n\n"
"A safe and powerful way to clean up your Open WebUI database\n"
"and reclaim disk space.",
border_style="cyan"
))
console.print()
def check_environment(self) -> bool:
"""Check if environment is properly configured."""
console.print("[bold]Checking environment...[/bold]")
try:
# Check database connection
users = Users.get_users()
console.print(f"[green]✓[/green] Database connection successful ({len(users['users'])} users found)")
# Initialize vector cleaner
self.vector_cleaner = get_vector_database_cleaner(
VECTOR_DB, VECTOR_DB_CLIENT, Path(CACHE_DIR)
)
console.print(f"[green]✓[/green] Vector database: {VECTOR_DB}")
console.print()
return True
except Exception as e:
console.print(f"[red]✗ Failed to connect to database: {e}[/red]")
console.print("\nMake sure:")
console.print(" • DATABASE_URL environment variable is set")
console.print(" • Database file exists and is accessible")
console.print(" • Open WebUI dependencies are installed")
return False
def show_main_menu(self) -> str:
"""Show main menu and get user choice."""
console.print("\n" + "=" * 70)
console.print("[bold cyan]Main Menu[/bold cyan]")
console.print("=" * 70)
console.print("\n[1] [green]Configure Settings[/green] - Set up what to delete")
console.print("[2] [yellow]Preview Changes[/yellow] - See what will be deleted (safe)")
console.print("[3] [red]Execute Pruning[/red] - Actually delete data (DESTRUCTIVE)")
console.print("[4] [blue]Help & Information[/blue] - Learn about prune operations")
console.print("[5] [dim]Exit[/dim]")
console.print()
choice = Prompt.ask(
"Choose an option",
choices=["1", "2", "3", "4", "5"],
default="1"
)
actions = {
"1": "configure",
"2": "preview",
"3": "execute",
"4": "help",
"5": "exit"
}
return actions[choice]
def configure_settings(self):
"""Interactive configuration menu."""
console.clear()
console.print(Panel.fit(
"[bold]Configuration Settings[/bold]",
border_style="blue"
))
while True:
console.print("\n[bold]Configuration Categories:[/bold]")
console.print("[1] User Account Deletion")
console.print("[2] Chat Deletion Settings")
console.print("[3] Orphaned Data Cleanup")
console.print("[4] Audio Cache Cleanup")
console.print("[5] System Optimization (VACUUM)")
console.print("[6] View Current Settings")
console.print("[7] Reset to Defaults")
console.print("[8] Back to Main Menu")
choice = Prompt.ask("Choose category", choices=["1", "2", "3", "4", "5", "6", "7", "8"])
if choice == "1":
self.configure_user_deletion()
elif choice == "2":
self.configure_chat_deletion()
elif choice == "3":
self.configure_orphaned_cleanup()
elif choice == "4":
self.configure_audio_cache()
elif choice == "5":
self.configure_vacuum()
elif choice == "6":
self.show_current_settings()
elif choice == "7":
self.form_data = PruneDataForm()
console.print("[green]Settings reset to defaults[/green]")
elif choice == "8":
break
def configure_user_deletion(self):
"""Configure inactive user deletion settings."""
console.print("\n[bold yellow]⚠ Warning: User Deletion is VERY DESTRUCTIVE[/bold yellow]")
console.print("Deleting users will cascade delete ALL their data:")
console.print(" • All their chats and messages")
console.print(" • All their files and uploads")
console.print(" • All their custom tools, functions, prompts")
console.print(" • All their knowledge bases")
console.print(" • Everything they created")
console.print()
if Confirm.ask("Do you want to enable inactive user deletion?"):
days = IntPrompt.ask(
"Delete users inactive for more than how many days?",
default=180
)
self.form_data.delete_inactive_users_days = days
if days < 30:
console.print("[red]⚠ WARNING: Less than 30 days is very aggressive![/red]")
console.print("You might accidentally delete users who are just on vacation.")
if not Confirm.ask("Are you SURE you want such a short period?"):
self.form_data.delete_inactive_users_days = None
return
self.form_data.exempt_admin_users = Confirm.ask(
"Exempt admin users from deletion? (STRONGLY RECOMMENDED)",
default=True
)
self.form_data.exempt_pending_users = Confirm.ask(
"Exempt pending/unapproved users from deletion?",
default=True
)
console.print(f"[green]✓[/green] Will delete users inactive for {days}+ days")
else:
self.form_data.delete_inactive_users_days = None
console.print("[green]User deletion disabled[/green]")
def configure_chat_deletion(self):
"""Configure chat deletion settings."""
console.print("\n[bold]Chat Deletion Settings[/bold]")
console.print("You can delete chats based on age (when they were last updated)")
console.print()
if Confirm.ask("Enable age-based chat deletion?"):
days = IntPrompt.ask(
"Delete chats older than how many days?",
default=90
)
self.form_data.days = days
self.form_data.exempt_archived_chats = Confirm.ask(
"Keep archived chats even if old?",
default=True
)
self.form_data.exempt_chats_in_folders = Confirm.ask(
"Keep chats in folders/pinned even if old?",
default=False
)
console.print(f"[green]✓[/green] Will delete chats older than {days} days")
else:
self.form_data.days = None
console.print("[green]Age-based chat deletion disabled[/green]")
# Orphaned chats (from deleted users)
console.print("\n[bold]Orphaned Chats[/bold]")
console.print("Chats from deleted users that no longer have an owner")
self.form_data.delete_orphaned_chats = Confirm.ask(
"Delete orphaned chats?",
default=True
)
self.form_data.delete_orphaned_folders = Confirm.ask(
"Delete orphaned folders?",
default=True
)
def configure_orphaned_cleanup(self):
"""Configure orphaned data cleanup."""
console.print("\n[bold]Orphaned Data Cleanup[/bold]")
console.print("Clean up workspace items from deleted users")
console.print()
table = Table(show_header=True, header_style="bold")
table.add_column("Item Type")
table.add_column("Current Setting")
table.add_column("Description")
items = [
("Knowledge Bases", "delete_orphaned_knowledge_bases", "User knowledge bases"),
("Tools", "delete_orphaned_tools", "Custom tools"),
("Functions", "delete_orphaned_functions", "Actions, Pipes, Filters"),
("Prompts", "delete_orphaned_prompts", "Custom prompts"),
("Models", "delete_orphaned_models", "Custom model configs"),
("Notes", "delete_orphaned_notes", "User notes"),
("Skills", "delete_orphaned_skills", "Custom skills"),
("Chat Messages", "delete_orphaned_chat_messages", "Analytics data from deleted chats"),
]
for name, attr, desc in items:
current = "✓ Enabled" if getattr(self.form_data, attr) else "✗ Disabled"
table.add_row(name, current, desc)
console.print(table)
console.print()
if Confirm.ask("Would you like to change these settings?"):
for name, attr, desc in items:
current = getattr(self.form_data, attr)
new_value = Confirm.ask(
f"Delete orphaned {name}?",
default=current
)
setattr(self.form_data, attr, new_value)
console.print("[green]✓ Orphaned data settings updated[/green]")
def configure_audio_cache(self):
"""Configure audio cache cleanup."""
console.print("\n[bold]Audio Cache Cleanup[/bold]")
console.print("Remove old TTS (text-to-speech) and STT (speech-to-text) files")
console.print()
if Confirm.ask("Enable audio cache cleanup?", default=True):
days = IntPrompt.ask(
"Delete audio files older than how many days?",
default=30
)
self.form_data.audio_cache_max_age_days = days
console.print(f"[green]✓[/green] Will delete audio cache older than {days} days")
else:
self.form_data.audio_cache_max_age_days = None
console.print("[green]Audio cache cleanup disabled[/green]")
def configure_vacuum(self):
"""Configure VACUUM optimization."""
console.print("\n[bold red]⚠ DATABASE VACUUM WARNING[/bold red]")
console.print()
console.print("VACUUM reclaims disk space by rebuilding the database file.")
console.print()
console.print("[bold yellow]⚠ Critical Warnings:[/bold yellow]")
console.print(" • LOCKS the entire database during execution")
console.print(" • ALL users will experience errors during VACUUM")
console.print(" • Can take 5-30+ minutes depending on database size")
console.print(" • Should ONLY be run during maintenance windows")
console.print(" • Not required for routine cleanups")
console.print()
self.form_data.run_vacuum = Confirm.ask(
"[bold]Enable VACUUM optimization?[/bold]",
default=False
)
if self.form_data.run_vacuum:
console.print("[yellow]⚠ VACUUM enabled - ensure this is a maintenance window![/yellow]")
else:
console.print("[green]VACUUM disabled (recommended for routine use)[/green]")
def show_current_settings(self):
"""Display current configuration."""
console.clear()
console.print(Panel.fit(
"[bold]Current Configuration[/bold]",
border_style="cyan"
))
# User deletion
console.print("\n[bold cyan]User Account Deletion:[/bold cyan]")
if self.form_data.delete_inactive_users_days is not None:
console.print(f" [yellow]Enabled[/yellow] - Delete users inactive for {self.form_data.delete_inactive_users_days}+ days")
console.print(f" Exempt admins: {'Yes' if self.form_data.exempt_admin_users else 'No'}")
console.print(f" Exempt pending: {'Yes' if self.form_data.exempt_pending_users else 'No'}")
else:
console.print(" [dim]Disabled[/dim]")
# Chat deletion
console.print("\n[bold cyan]Chat Deletion:[/bold cyan]")
if self.form_data.days is not None:
console.print(f" [yellow]Enabled[/yellow] - Delete chats older than {self.form_data.days} days")
console.print(f" Exempt archived: {'Yes' if self.form_data.exempt_archived_chats else 'No'}")
console.print(f" Exempt in folders: {'Yes' if self.form_data.exempt_chats_in_folders else 'No'}")
else:
console.print(" [dim]Disabled[/dim]")
# Orphaned data
console.print("\n[bold cyan]Orphaned Data Cleanup:[/bold cyan]")
orphaned_items = [
("Chats", self.form_data.delete_orphaned_chats),
("Knowledge Bases", self.form_data.delete_orphaned_knowledge_bases),
("Tools", self.form_data.delete_orphaned_tools),
("Functions", self.form_data.delete_orphaned_functions),
("Prompts", self.form_data.delete_orphaned_prompts),
("Models", self.form_data.delete_orphaned_models),
("Notes", self.form_data.delete_orphaned_notes),
("Skills", self.form_data.delete_orphaned_skills),
("Folders", self.form_data.delete_orphaned_folders),
("Chat Messages", self.form_data.delete_orphaned_chat_messages),
]
for name, enabled in orphaned_items:
status = "[green]✓[/green]" if enabled else "[dim]✗[/dim]"
console.print(f" {status} {name}")
# Audio cache
console.print("\n[bold cyan]Audio Cache:[/bold cyan]")
if self.form_data.audio_cache_max_age_days is not None:
console.print(f" [yellow]Enabled[/yellow] - Delete files older than {self.form_data.audio_cache_max_age_days} days")
else:
console.print(" [dim]Disabled[/dim]")
# VACUUM
console.print("\n[bold cyan]System Optimization:[/bold cyan]")
if self.form_data.run_vacuum:
console.print(" [red]⚠ VACUUM ENABLED[/red] - Will lock database!")
else:
console.print(" [dim]VACUUM disabled[/dim]")
console.print()
Prompt.ask("Press Enter to continue")
def run_preview(self):
"""Run preview and show results."""
console.clear()
console.print(Panel.fit(
"[bold yellow]Preview Mode[/bold yellow]\n"
"Calculating what would be deleted...",
border_style="yellow"
))
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
console=console
) as progress:
task = progress.add_task("Analyzing database...", total=None)
try:
# Build sets
knowledge_bases = Knowledges.get_knowledge_bases()
all_users = Users.get_users()["users"]
active_user_ids = {user.id for user in all_users}
active_kb_ids = {
kb.id
for kb in knowledge_bases
if kb.user_id in active_user_ids
}
active_file_ids = get_active_file_ids(knowledge_bases, active_user_ids)
orphaned_counts = count_orphaned_records(self.form_data, active_file_ids, active_user_ids)
result = PrunePreviewResult(
inactive_users=count_inactive_users(
self.form_data.delete_inactive_users_days,
self.form_data.exempt_admin_users,
self.form_data.exempt_pending_users,
all_users,
),
old_chats=count_old_chats(
self.form_data.days,
self.form_data.exempt_archived_chats,
self.form_data.exempt_chats_in_folders,
),
orphaned_chats=orphaned_counts["chats"],
orphaned_files=orphaned_counts["files"],
orphaned_tools=orphaned_counts["tools"],
orphaned_functions=orphaned_counts["functions"],
orphaned_prompts=orphaned_counts["prompts"],
orphaned_knowledge_bases=orphaned_counts["knowledge_bases"],
orphaned_models=orphaned_counts["models"],
orphaned_notes=orphaned_counts["notes"],
orphaned_skills=orphaned_counts["skills"],
orphaned_folders=orphaned_counts["folders"],
orphaned_uploads=count_orphaned_uploads(active_file_ids),
orphaned_vector_collections=self.vector_cleaner.count_orphaned_collections(
active_file_ids, active_kb_ids, active_user_ids
),
audio_cache_files=count_audio_cache_files(
self.form_data.audio_cache_max_age_days
),
orphaned_chat_messages=orphaned_counts["chat_messages"],
)
progress.update(task, completed=True)
except Exception as e:
console.print(f"\n[red]Error during preview: {e}[/red]")
log.exception("Preview failed")
return
# Display results
console.print()
self.display_preview_results(result)
console.print()
Prompt.ask("Press Enter to continue")
def display_preview_results(self, result: PrunePreviewResult):
"""Display preview results in a beautiful table."""
console.print("\n" + "=" * 70)
console.print("[bold]PREVIEW RESULTS - What Will Be Deleted[/bold]")
console.print("=" * 70)
if not result.has_items():
console.print("\n[green]✓ Nothing to delete - your database is clean![/green]")
return
summary = result.get_summary_dict()
for category, items in summary.items():
# Skip empty categories
if not any(count > 0 for count in items.values()):
continue
console.print(f"\n[bold cyan]{category}:[/bold cyan]")
for name, count in items.items():
if count > 0:
console.print(f" [yellow]{count:,}[/yellow] {name}")
console.print("\n" + "=" * 70)
console.print(f"[bold red]TOTAL ITEMS: {result.total_items():,}[/bold red]")
console.print("=" * 70)
def confirm_execution(self) -> bool:
"""Confirm execution with multiple warnings."""
console.clear()
console.print(Panel.fit(
"[bold red]⚠ DESTRUCTIVE OPERATION WARNING ⚠[/bold red]\n\n"
"You are about to PERMANENTLY DELETE data from your database.\n"
"This action CANNOT be undone!",
border_style="red",
title="[bold]DANGER[/bold]"
))
console.print("\n[bold yellow]Before proceeding:[/bold yellow]")
console.print(" [red]✓[/red] Have you created a database backup?")
console.print(" [red]✓[/red] Have you reviewed the preview?")
console.print(" [red]✓[/red] Are you sure you want to proceed?")
console.print()
if not Confirm.ask("[bold red]Do you want to proceed with deletion?[/bold red]", default=False):
console.print("\n[green]Cancelled - no changes made[/green]")
return False
# Second confirmation
console.print("\n[bold red]FINAL CONFIRMATION[/bold red]")
console.print("Type 'DELETE' (all caps) to confirm:")
confirmation = Prompt.ask("Type DELETE to continue")
if confirmation != "DELETE":
console.print("\n[green]Cancelled - no changes made[/green]")
return False
return True
def run_execution(self):
"""Execute the actual pruning operation."""
console.print("\n[bold red]Starting pruning operation...[/bold red]")
# Acquire lock
if not PruneLock.acquire():
console.print("\n[red]ERROR: Another prune operation is already in progress[/red]")
console.print("Please wait for it to complete.")
return
try:
self.execute_prune_stages()
finally:
PruneLock.release()
console.print("\n[bold green]✓ Pruning operation completed successfully![/bold green]")
Prompt.ask("\nPress Enter to continue")
def execute_prune_stages(self):
"""Execute all prune stages with progress display."""
with Progress(console=console) as progress:
# Stage 0: Inactive users
if self.form_data.delete_inactive_users_days is not None:
task = progress.add_task("Deleting inactive users...", total=None)
deleted = delete_inactive_users(
self.form_data.delete_inactive_users_days,
self.vector_cleaner,
self.form_data.exempt_admin_users,
self.form_data.exempt_pending_users,
)
progress.update(task, completed=True)
console.print(f"[green]✓[/green] Deleted {deleted} inactive users")
# Stage 1: Old chats
if self.form_data.days is not None:
task = progress.add_task("Deleting old chats...", total=None)
cutoff_time = int(time.time()) - (self.form_data.days * 86400)
deleted = 0
with get_db() as db:
for chat in Chats.get_chats(db=db):
if chat.updated_at < cutoff_time:
if self.form_data.exempt_archived_chats and chat.archived:
continue
if self.form_data.exempt_chats_in_folders and (
getattr(chat, "folder_id", None) is not None
or getattr(chat, "pinned", False)
):
continue
Chats.delete_chat_by_id(chat.id, db=db)
deleted += 1
progress.update(task, completed=True)
console.print(f"[green]✓[/green] Deleted {deleted} old chats")
# Stage 2-3: Orphaned data
task = progress.add_task("Building preservation set...", total=None)
active_user_ids = {user.id for user in Users.get_users()["users"]}
knowledge_bases = Knowledges.get_knowledge_bases()
active_kb_ids = {kb.id for kb in knowledge_bases if kb.user_id in active_user_ids}
active_file_ids = get_active_file_ids(knowledge_bases, active_user_ids)
progress.update(task, completed=True)
# Delete orphaned files
task = progress.add_task("Deleting orphaned files...", total=None)
deleted_files = 0
# Use shared database session for efficient bulk deletion
with get_db() as db:
for file_record in Files.get_files(db=db):
should_delete = (
file_record.id not in active_file_ids
or file_record.user_id not in active_user_ids
)
if should_delete:
if safe_delete_file_by_id(file_record.id, self.vector_cleaner, db=db):
deleted_files += 1
progress.update(task, completed=True)
console.print(f"[green]✓[/green] Deleted {deleted_files} orphaned files")
# Delete other orphaned data - use shared session for each type
# Knowledge bases
if self.form_data.delete_orphaned_knowledge_bases:
task = progress.add_task("Deleting orphaned knowledge bases...", total=None)
deleted = 0
with get_db() as db:
for kb in Knowledges.get_knowledge_bases(db=db):
if kb.user_id not in active_user_ids:
self.vector_cleaner.delete_collection(kb.id)
Knowledges.delete_knowledge_by_id(kb.id, db=db)
deleted += 1
progress.update(task, completed=True)
console.print(f"[green]✓[/green] Deleted {deleted} orphaned knowledge bases")
# Chats
if self.form_data.delete_orphaned_chats:
task = progress.add_task("Deleting orphaned chats...", total=None)
deleted = 0
with get_db() as db:
for chat in Chats.get_chats(db=db):
if chat.user_id not in active_user_ids:
Chats.delete_chat_by_id(chat.id, db=db)
deleted += 1
progress.update(task, completed=True)
console.print(f"[green]✓[/green] Deleted {deleted} orphaned chats")
# Tools
if self.form_data.delete_orphaned_tools:
task = progress.add_task("Deleting orphaned tools...", total=None)
deleted = 0
with get_db() as db:
for tool in Tools.get_tools(db=db):
if tool.user_id not in active_user_ids:
Tools.delete_tool_by_id(tool.id, db=db)
deleted += 1
progress.update(task, completed=True)
console.print(f"[green]✓[/green] Deleted {deleted} orphaned tools")
# Functions
if self.form_data.delete_orphaned_functions:
task = progress.add_task("Deleting orphaned functions...", total=None)
deleted = 0
with get_db() as db:
for function in Functions.get_functions(db=db):
if function.user_id not in active_user_ids:
Functions.delete_function_by_id(function.id, db=db)
deleted += 1
progress.update(task, completed=True)
console.print(f"[green]✓[/green] Deleted {deleted} orphaned functions")
# Prompts
if self.form_data.delete_orphaned_prompts:
task = progress.add_task("Deleting orphaned prompts...", total=None)
deleted = 0
with get_db() as db:
for prompt in Prompts.get_prompts(db=db):
if prompt.user_id not in active_user_ids:
Prompts.delete_prompt_by_command(prompt.command, db=db)
deleted += 1
progress.update(task, completed=True)
console.print(f"[green]✓[/green] Deleted {deleted} orphaned prompts")
# Models
if self.form_data.delete_orphaned_models:
task = progress.add_task("Deleting orphaned models...", total=None)
deleted = 0
with get_db() as db:
for model in Models.get_all_models(db=db):
if model.user_id not in active_user_ids:
Models.delete_model_by_id(model.id, db=db)
deleted += 1
progress.update(task, completed=True)
console.print(f"[green]✓[/green] Deleted {deleted} orphaned models")
# Notes
if self.form_data.delete_orphaned_notes:
task = progress.add_task("Deleting orphaned notes...", total=None)
deleted = 0
with get_db() as db:
for note in Notes.get_notes(db=db):
if note.user_id not in active_user_ids:
Notes.delete_note_by_id(note.id, db=db)
deleted += 1
progress.update(task, completed=True)
console.print(f"[green]✓[/green] Deleted {deleted} orphaned notes")
# Skills
if self.form_data.delete_orphaned_skills:
task = progress.add_task("Deleting orphaned skills...", total=None)
deleted = 0
with get_db() as db:
for skill in Skills.get_skills(db=db):
if skill.user_id not in active_user_ids:
Skills.delete_skill_by_id(skill.id, db=db)
deleted += 1
progress.update(task, completed=True)
console.print(f"[green]✓[/green] Deleted {deleted} orphaned skills")
# Folders
if self.form_data.delete_orphaned_folders:
task = progress.add_task("Deleting orphaned folders...", total=None)
deleted = 0
with get_db() as db:
for folder in self._get_all_folders_safe(db=db):
if folder.user_id not in active_user_ids:
Folders.delete_folder_by_id_and_user_id(folder.id, folder.user_id, db=db)
deleted += 1
progress.update(task, completed=True)
console.print(f"[green]✓[/green] Deleted {deleted} orphaned folders")
# Orphaned chat messages
if self.form_data.delete_orphaned_chat_messages:
task = progress.add_task("Deleting orphaned chat messages...", total=None)
deleted = delete_orphaned_chat_messages()
progress.update(task, completed=True)
console.print(f"[green]✓[/green] Deleted {deleted} orphaned chat messages")
# Stage 4: Cleanup physical files
task = progress.add_task("Cleaning up orphaned uploads...", total=None)
final_active_user_ids = {user.id for user in Users.get_users()["users"]}
final_knowledge_bases = Knowledges.get_knowledge_bases()
final_active_kb_ids = {kb.id for kb in final_knowledge_bases if kb.user_id in final_active_user_ids}
final_active_file_ids = get_active_file_ids(final_knowledge_bases, final_active_user_ids)
deleted_uploads = cleanup_orphaned_uploads(final_active_file_ids)
progress.update(task, completed=True)
console.print(f"[green]✓[/green] Deleted {deleted_uploads} orphaned upload files")
task = progress.add_task("Cleaning up vector collections...", total=None)
deleted_vector, error = self.vector_cleaner.cleanup_orphaned_collections(
final_active_file_ids, final_active_kb_ids, final_active_user_ids
)
progress.update(task, completed=True)
console.print(f"[green]✓[/green] Deleted {deleted_vector} orphaned vector collections")
# Audio cache
if self.form_data.audio_cache_max_age_days is not None:
task = progress.add_task("Cleaning audio cache...", total=None)
deleted_audio = cleanup_audio_cache(self.form_data.audio_cache_max_age_days)
progress.update(task, completed=True)
console.print(f"[green]✓[/green] Deleted {deleted_audio} audio cache files")
# VACUUM
if self.form_data.run_vacuum:
task = progress.add_task("Running VACUUM (this may take a while)...", total=None)
try:
with get_db() as db:
engine = db.get_bind()
db_url = str(engine.url)
if 'postgresql' in db_url:
# PostgreSQL: VACUUM requires autocommit mode (no transaction)
raw_connection = engine.raw_connection()
try:
raw_connection.set_isolation_level(0) # AUTOCOMMIT mode
cursor = raw_connection.cursor()
cursor.execute("VACUUM ANALYZE")
cursor.close()
raw_connection.commit()
console.print("[green]✓[/green] Vacuumed PostgreSQL main database")
finally:
raw_connection.close()
else:
# SQLite: Can run in transaction
db.execute(text("VACUUM"))
console.print("[green]✓[/green] Vacuumed main database")
if isinstance(self.vector_cleaner, ChromaDatabaseCleaner):
# Log size before VACUUM
size_before_mb = self.vector_cleaner.chroma_db_path.stat().st_size / (1024 * 1024)
with sqlite3.connect(str(self.vector_cleaner.chroma_db_path)) as conn:
conn.execute("VACUUM")
# Log size after VACUUM
size_after_mb = self.vector_cleaner.chroma_db_path.stat().st_size / (1024 * 1024)
freed_mb = size_before_mb - size_after_mb
console.print(f"[green]✓[/green] Vacuumed ChromaDB ({size_before_mb:.1f}MB → {size_after_mb:.1f}MB, freed {freed_mb:.1f}MB)")
elif isinstance(self.vector_cleaner, PGVectorDatabaseCleaner) and self.vector_cleaner.session:
engine = self.vector_cleaner.session.get_bind()
raw_connection = engine.raw_connection()
try:
raw_connection.set_isolation_level(0) # AUTOCOMMIT mode
cursor = raw_connection.cursor()
cursor.execute("VACUUM ANALYZE")
cursor.close()
raw_connection.commit()
console.print("[green]✓[/green] Vacuumed PostgreSQL vector database")
finally:
raw_connection.close()
except Exception as e:
console.print(f"[yellow]⚠ VACUUM failed: {e}[/yellow]")
progress.update(task, completed=True)
def show_help(self):
"""Show help information."""
console.clear()
help_text = """
# Open WebUI Prune Tool Help
## What This Tool Does
This interactive tool helps you clean up your Open WebUI database by:
- Deleting inactive user accounts
- Removing old conversations
- Cleaning up orphaned data from deleted users
- Removing unused files and uploads
- Cleaning vector database collections
- Reclaiming disk space
## Safety Features
✓ **Dry-run preview** - See what will be deleted before committing
✓ **Multiple confirmations** - Prevents accidental deletion
✓ **Granular control** - Choose exactly what to clean
✓ **File-based locking** - Prevents concurrent operations
✓ **Comprehensive logging** - Track all operations
## Recommended Workflow
1. **Configure settings** - Choose what to clean
2. **Run preview** - See what will be deleted
3. **Backup database** - Create a backup before executing
4. **Execute** - Perform the actual cleanup
5. **Verify** - Check logs and database size
## Warning Categories
🟡 **Yellow** - Safe, reversible, or preview
🟠 **Orange** - Potentially destructive, needs care
🔴 **Red** - Very destructive, backup required
## Getting Help
- Review the README.md for detailed documentation
- Check ANALYSIS.md for technical details
- Review logs for operation history
"""
console.print(Markdown(help_text))
console.print()
Prompt.ask("Press Enter to continue")
def main():
"""Main entry point."""
# Set up logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[logging.StreamHandler()]
)
try:
ui = InteractivePruneUI()
return ui.run()
except KeyboardInterrupt:
console.print("\n\n[yellow]Interrupted by user[/yellow]")
return 130
except Exception as e:
console.print(f"\n[red]Fatal error: {e}[/red]")
log.exception("Fatal error")
return 1
if __name__ == "__main__":
sys.exit(main())