Commit 3e3067a9 authored by Your Name's avatar Your Name

Fix --remove-model to remove entire HF repository directories

- Updated remove_cached_model() to remove entire repo directories when matching by repo_id
- Previously only removed individual files, now removes complete repository cache
- Handles both files and directories in removal process
- More thorough cleanup of HuggingFace cached models
parent c93d4a6b
...@@ -329,14 +329,21 @@ def remove_cached_model(match_term: str) -> List[Tuple[str, str, int]]: ...@@ -329,14 +329,21 @@ def remove_cached_model(match_term: str) -> List[Tuple[str, str, int]]:
# Check if match_term matches any repo_id # Check if match_term matches any repo_id
for repo in cache_info.repos: for repo in cache_info.repos:
if match_term.lower() in repo.repo_id.lower(): if match_term.lower() in repo.repo_id.lower():
# Found matching repo, add all its files # Found matching repo - remove the entire repo directory
for revision in repo.revisions: # This is more thorough than removing individual files
for file_info in revision.files: repo_dir = os.path.join(cache_dir, f"models--{repo.repo_id.replace('/', '--')}")
filepath = os.path.join(cache_dir, file_info.file_path) if os.path.exists(repo_dir):
if os.path.exists(filepath): # Calculate total size of all files in the repo
size = os.path.getsize(filepath) total_size = 0
rel_path = file_info.file_path for root, dirs, files in os.walk(repo_dir):
all_matching.append((cache_name, rel_path, filepath, size)) for f in files:
filepath = os.path.join(root, f)
try:
total_size += os.path.getsize(filepath)
except OSError:
pass
# Add the entire directory for removal
all_matching.append((cache_name, f"models--{repo.repo_id.replace('/', '--')}", repo_dir, total_size))
break # Only match one repo per search term break # Only match one repo per search term
except ImportError: except ImportError:
# huggingface_hub not available, fall back to filename search # huggingface_hub not available, fall back to filename search
...@@ -363,13 +370,19 @@ def remove_cached_model(match_term: str) -> List[Tuple[str, str, int]]: ...@@ -363,13 +370,19 @@ def remove_cached_model(match_term: str) -> List[Tuple[str, str, int]]:
size = os.path.getsize(filepath) size = os.path.getsize(filepath)
all_matching.append((cache_name, f, filepath, size)) all_matching.append((cache_name, f, filepath, size))
# Remove matching files # Remove matching files/directories
removed = [] removed = []
for cache_name, filename, filepath, size in all_matching: for cache_name, filename, filepath, size in all_matching:
try: try:
os.remove(filepath) if os.path.isdir(filepath):
# Remove entire directory tree
shutil.rmtree(filepath)
print(f" Deleted: [{cache_name}] {filename}/ (directory)")
else:
# Remove single file
os.remove(filepath)
print(f" Deleted: [{cache_name}] {filename}")
removed.append((cache_name, filename, size)) removed.append((cache_name, filename, size))
print(f" Deleted: [{cache_name}] {filename}")
except Exception as e: except Exception as e:
print(f" Error deleting [{cache_name}] {filename}: {e}") print(f" Error deleting [{cache_name}] {filename}: {e}")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment