Last active
June 23, 2025 20:09
-
-
Save JoSSte/fadd944fcf89e17792e1e1867dc3af61 to your computer and use it in GitHub Desktop.
Clean up development files to save disk space
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import shutil | |
from pathlib import Path | |
# Script to clean up caches prior to backing up, copying or moving development folder. | |
# shows sizes and asks for confirmation prior to deleting. | |
TARGET_DIR_NAMES = { | |
"vendor" | |
#, "cache" | |
, "node_modules" | |
, ".angular" | |
} | |
def get_directory_size(path: Path) -> int: | |
total_size = 0 | |
for dirpath, dirnames, filenames in os.walk(path): | |
for f in filenames: | |
try: | |
fp = Path(dirpath) / f | |
if fp.is_file(): | |
total_size += fp.stat().st_size | |
except Exception as e: | |
print(f"Could not access {fp}: {e}") | |
return total_size | |
def human_readable_size(size_in_bytes): | |
for unit in ['B', 'KB', 'MB', 'GB', 'TB']: | |
if size_in_bytes < 1024: | |
return f"{size_in_bytes:.2f} {unit}" | |
size_in_bytes /= 1024 | |
return f"{size_in_bytes:.2f} PB" | |
def find_target_dirs(start_path="."): | |
matches = [] | |
for dirpath, dirnames, _ in os.walk(start_path): | |
for dirname in dirnames: | |
if dirname in TARGET_DIR_NAMES: | |
full_path = Path(dirpath) / dirname | |
matches.append(full_path) | |
return matches | |
def main(): | |
print("Scanning for target directories...\n") | |
found_dirs = find_target_dirs() | |
if not found_dirs: | |
print("No matching directories found.") | |
return | |
total_size = 0 | |
dir_sizes = [] | |
for d in found_dirs: | |
size = get_directory_size(d) | |
total_size += size | |
dir_sizes.append((d, size)) | |
print("\nFound the following directories:") | |
for path, size in dir_sizes: | |
print(f"- {path} → {human_readable_size(size)}") | |
print(f"\nTotal space used: {human_readable_size(total_size)}") | |
confirm = input("\nDo you want to delete all these directories? [y/N]: ").strip().lower() | |
if confirm == 'y': | |
for path, _ in dir_sizes: | |
try: | |
shutil.rmtree(path) | |
print(f"Deleted: {path}") | |
except Exception as e: | |
print(f"Failed to delete {path}: {e}") | |
print("\nAll selected directories have been deleted.") | |
else: | |
print("Aborted. No directories were deleted.") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment