AstonProject / src /clear_cache.py
FaroukTomori's picture
Upload clear_cache.py
4fa55d2 verified
#!/usr/bin/env python3
"""
Cache clearing script for Hugging Face Spaces
Removes stuck lock files and clears cache to fix PermissionError
"""
import os
import shutil
import subprocess
from pathlib import Path
def clear_hf_cache():
"""Clear Hugging Face cache and remove lock files"""
# Common cache locations
cache_paths = [
"/.cache/huggingface",
"/tmp/huggingface",
os.path.expanduser("~/.cache/huggingface"),
os.path.expanduser("~/.huggingface")
]
print("🧹 Clearing Hugging Face cache...")
for cache_path in cache_paths:
if os.path.exists(cache_path):
print(f"πŸ“ Found cache at: {cache_path}")
# Remove lock files
lock_files = list(Path(cache_path).rglob("*.lock"))
for lock_file in lock_files:
try:
os.remove(lock_file)
print(f"πŸ”“ Removed lock file: {lock_file}")
except Exception as e:
print(f"⚠️ Could not remove {lock_file}: {e}")
# Clear the cache directory
try:
shutil.rmtree(cache_path)
print(f"πŸ—‘οΈ Cleared cache: {cache_path}")
except Exception as e:
print(f"⚠️ Could not clear {cache_path}: {e}")
else:
print(f"❌ Cache not found at: {cache_path}")
print("βœ… Cache clearing complete!")
if __name__ == "__main__":
clear_hf_cache()