Building a Disk Cleanup Tool with LLMs: Preview First, Delete Later
When a server disk fills up, you need to know what's taking the space and what can be safely removed. This article shows how to use a large language model to generate a disk analysis and cleanup tool in Python, how to write clear requirements, how to review the generated code, and what safety guards a deletion feature must have.
When Disk Space Runs Out, Find Out Where It Went First
A server’s disk usage suddenly spikes to 95%: logs can’t be written, builds fail. At that moment you need two things: figure out which directories consume the most space, and identify which files are safe to delete. Hand-rolling commands with du and find works, but you end up piecing the command together every time, and edge cases are easy to miss.
Handing this kind of task to an LLM usually produces a working script within minutes. That said, model-generated code isn’t always reliable — especially code that deletes files. Using a disk analysis and cleanup tool as an example, this article covers how to phrase the request, how to review the generated result, and what protections the deletion logic should include.
Write the Requirements Down Clearly
The tool needs to do four things:
- Compute the recursive total size of every subdirectory under a given directory, and list the top N items sorted by size in descending order.
- Match candidate files by filename patterns, such as
*.logand*.tmp. - Preview only by default — no deletion. Deletion runs only when a confirmation flag is passed and the user types
yes. - Log every deletion, recording both successful and failed files.
The more specific the requirements, the closer the generated code will be to production-ready. Here is a prompt template you can adapt directly:
Write a command-line disk analysis and cleanup tool in Python 3, targeting Linux.Input: scan root directory (parameter root), number of items to display (--top, default 20), filename patterns in cleanup mode (--pattern, can be specified multiple times).Output: recursive size (MB) of each directory, sorted descending by size; in cleanup mode, list matched files and their total size.Safety constraints: preview only by default; deletion requires the --confirm flag plus typing yes; do not follow symbolic links; skip system directories such as /etc and /usr; write all deletion operations to cleanup.log.Acceptance criteria: a parent directory's size equals its own file sizes plus the sum of all subdirectory sizes; a single file deletion failure must not abort the process
