Generating a Disk Cleanup Tool with One LLM Prompt: Practice and Insights

    Disk space exhaustion is a frequent challenge for developers. This article explores using Large Language Models (LLMs) to generate a disk analysis and cleanup script from a single prompt. It covers prompt engineering strategies, core implementation logic, safe cleanup principles, and practical observations on LLM code generation capabilities and API performance.

    1. Why Generate a Disk Cleanup Tool with One Prompt?

    Running out of disk space is one of the most common headaches in development. Build caches, container images, log files, dependency directories, and temporary downloads accumulate continuously, filling up terabytes of storage unnoticed. The typical manual approach of running du and df commands layer by layer is tedious and prone to missing items.

    The advent of LLM coding assistants has changed this landscape. As long as the requirements are clearly described, the model can generate a runnable analysis and cleanup script, automating the “investigate—locate—clean” workflow. This task is highly suitable for LLMs for three reasons:

    1. Clear requirement boundaries: The goals, inputs, and outputs of disk analysis are well-defined, minimizing ambiguity for the model.
    2. Mature technical solutions: Directory traversal, file size statistics, and rule-based deletion are standard techniques that do not rely on obscure knowledge.
    3. Controllable error margins: The analysis phase is read-only. Even if the script is imperfect, it causes no damage, allowing for rapid iteration.

    2. Writing the Prompt: From One Sentence to a Usable Program

    The initial requirement can be as simple as: “My disk is full, help me write a disk space analysis and cleanup program.” However, to achieve high-quality results, it is recommended to include the following elements in the prompt:

    • Environment: Operating system, scripting language preference (e.g., Python, Shell).
    • Analysis dimensions: Aggregate size by directory, statistics by file type, filter by modification time.
    • Output format: CLI table, Top N largest files list, visualized proportions.
    • Cleanup strategy: List candidate items for manual confirmation only, or support automatic deletion; whether a recycle bin mechanism is needed.

    Here is a more comprehensive prompt example:

    1. Write a disk space analysis tool in Python:
    2. 1. Recursively scan from a specified root directory and calculate the total size of each top-level subdirectory;
    3. 2. Output the top 20 largest directories and the top 20 largest files;
    4. 3. Identify common cleanable directories (caches, temporary files, logs) and list them separately;
    5. 4. Provide a --dry-run mode to preview before cleanup, executing deletion only after confirmation.

    With these constraints, the model’s generated code can often run directly, saving multiple rounds of debugging.

    3. Core Implementation Logic

    Below is a simplified logic illustration showing how the model typically organizes code:

    1. import os
    2. from pathlib import Path
    3. def dir_size(path: Path) -> int:
    4. total = 0
    5. for p in path.rglob('*'):
    6. try:
    7. if p.is_file():
    8. total += p.stat().st_size
    9. except (PermissionError, OSError):
    10. continue
    11. return total
    12. def top_dirs(root: Path, n=20):
    13. sizes = [(d, dir_size(d)) for d in root.iterdir() if d.is_dir()]
    14. return sorted(sizes, key=lambda x: x[1], reverse=True)[:n]

    The cleanup section should follow a three-stage process: “analyze first, then confirm, then delete,” and retain the --dry-run preview capability to prevent accidental deletion.

    4. Basic Principles for Safe Cleanup

    When allowing the model to generate deletion logic, manual review is essential, adhering to the following principles:

    1. Whitelist priority: Only clean explicitly safe directories, such as various caches, temp directories, and package manager caches.
    2. Never recursively force-delete root paths: Validate path legality for rm -rf operations to prevent accidental deletion of system directories.
    3. Keep previews and logs: Record the file path and size for every deletion for traceability.
    4. Confirm large files first: Files exceeding a threshold should prompt manual judgment rather than being silently deleted.

    5. Observations on LLM Code Generation Capabilities

    From such practices, current mainstream LLMs exhibit several common characteristics in code generation:

    • Concise and efficient responses: Excellent models skip lengthy internal reasoning and quickly provide well-structured code, ensuring a smooth interactive experience.
    • High completion rate for general tasks: Scripting and tooling requirements can generally be generated in one go. Complex requirements can reach a usable state after two or three iterations.
    • Human oversight is still required: For risky operations involving deletion, permissions, or concurrency, models do not proactively consider all edge cases. Code review is indispensable.

    6. Cloud API Performance Experience

    If calling the model via an API in programming tools, the actual experience also depends on server-side performance. Common issues include:

    • Throughput fluctuations: The response speed of the same model may vary significantly between free and paid tiers, dropping further during peak concurrency periods.
    • Connection stability: Occasional stream interruptions during long text generation require the client to support retry and resumable transfer capabilities.
    • The appeal of local deployment: Because of these factors, many developers look forward to local deployment solutions for open-source weight models. Running inference on their own hardware eliminates network and quota limitations, which is particularly valuable for high-frequency invocation scenarios like code completion.

    7. Conclusion

    Disk cleanup is a small yet typical scenario that effectively tests the practical value of LLMs: clear requirements, universal solutions, and controllable risks. A single prompt can yield a usable productivity tool. For developers, mastering the workflow of “clearly describing requirements + manual review of risky operations + preview before execution” yields more long-term benefits than memorizing any specific script. As model capabilities continue to improve and deployment methods become more flexible, this development approach of “getting a tool from a single prompt” will become the norm in daily programming.