Skip to content

Commit 0885647

Browse files
author
Sebastian Braun
committed
feat(cli): add empty subdirectory cleanup to add-all command
When auto_delete_added_files is enabled, the add-all command now recursively cleans up empty subdirectories in raw/ after file deletion. Directories are deleted from deepest to shallowest to ensure proper cleanup. Added summary output showing count of cleaned directories.
1 parent 680c04b commit 0885647

1 file changed

Lines changed: 38 additions & 5 deletions

File tree

openkb/cli.py

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,33 @@ def _delete_if_auto_cleanup_enabled(
478478
return False
479479

480480

481+
def _cleanup_empty_directories(start_dir: Path) -> int:
482+
"""Recursively delete empty directories under start_dir.
483+
484+
Walks from deepest subdirectories up, deleting directories that become
485+
empty after file cleanup.
486+
487+
Args:
488+
start_dir: Root directory to clean up (e.g., kb_dir / "raw").
489+
490+
Returns:
491+
Number of directories deleted.
492+
"""
493+
deleted_count = 0
494+
try:
495+
for directory in sorted(start_dir.rglob("*"), key=lambda p: len(p.parts), reverse=True):
496+
if directory.is_dir() and directory != start_dir:
497+
try:
498+
if not list(directory.iterdir()):
499+
directory.rmdir()
500+
deleted_count += 1
501+
except OSError:
502+
pass
503+
except Exception as exc:
504+
logger.warning(f"Error during directory cleanup: {exc}")
505+
return deleted_count
506+
507+
481508
def _add_single_file_locked(
482509
file_path: Path, kb_dir: Path, *, stage: bool = True, bundle=None
483510
) -> Literal["added", "skipped", "failed"]:
@@ -1199,7 +1226,8 @@ def add_all(ctx):
11991226
This command walks the ``raw/`` directory recursively for all supported
12001227
document types and ingests them into the KB. If ``auto_delete_added_files``
12011228
is enabled in config.yaml, files are automatically deleted after ingestion
1202-
(both on successful addition and on skip/duplicate).
1229+
(both on successful addition and on skip/duplicate), and empty subdirectories
1230+
are cleaned up.
12031231
12041232
Returns a summary of the operation (added, skipped, failed, deleted counts).
12051233
"""
@@ -1224,7 +1252,7 @@ def add_all(ctx):
12241252

12251253
config = resolve_effective_config(kb_dir)[0]
12261254
total = len(files)
1227-
added = skipped = failed = deleted = 0
1255+
added = skipped = failed = deleted = dirs_deleted = 0
12281256

12291257
click.echo(f"Processing {total} file(s) from raw/ directory...")
12301258
for i, f in enumerate(files, 1):
@@ -1239,9 +1267,14 @@ def add_all(ctx):
12391267
if _delete_if_auto_cleanup_enabled(f, outcome, config):
12401268
deleted += 1
12411269

1242-
click.echo(
1243-
f"\n\nSummary: Added: {added}, Skipped: {skipped}, Failed: {failed}, Deleted: {deleted}"
1244-
)
1270+
# Clean up empty subdirectories if auto-cleanup is enabled
1271+
if config.get("auto_delete_added_files", False):
1272+
dirs_deleted = _cleanup_empty_directories(raw_dir)
1273+
1274+
summary = f"Added: {added}, Skipped: {skipped}, Failed: {failed}, Deleted: {deleted}"
1275+
if dirs_deleted > 0:
1276+
summary += f", Empty dirs cleaned: {dirs_deleted}"
1277+
click.echo(f"\n\nSummary: {summary}")
12451278

12461279

12471280
def _stream_to_tty() -> bool:

0 commit comments

Comments
 (0)