πŸ€– New: AI Agent Crash Course β€” Presale €49View Course
Artificial IntelligenceBuilding in Public

37 GB Wasted: Why Automated Scripts Must Always Clean Up After Themselves

Jan Koch
Jan Koch
KI Experte & Berater
β€’3 min

This morning at 4:38 AM, I received an alert: the server disk was full. 100 percent. No space left.

My AI agent was trying to restart the wind energy platform β€” and failed. Because there was no space left.

The cause: an automated script had downloaded a 36 gigabyte ZIP export from the German energy registry a few days ago, processed it β€” and then just left it sitting there.

What happened?

The script worked fine technically. It downloaded the data, unpacked it, converted it to JSON, and restarted the platform. The problem was the last step: cleanup.

The temporary working directory /tmp/mastr-sync was only deleted on successful completion β€” at the end of the script, after the last log call. If the script crashed or was interrupted, the folder just stayed there.

Next sync run: another 36 GB. And so on.

The underestimated problem with temporary files

When you build automated processes β€” whether with Python, Bash, Node, or an AI agent β€” there's a pattern that keeps showing up:

  1. Download data βœ…
  2. Process data βœ…
  3. Save result βœ…
  4. Clean up temporary files ❌

Step 4 gets forgotten. Not out of laziness, but because it feels unimportant. The job is done, the result is there β€” why bother cleaning up?

Because disks are finite.

The fix: treat cleanup as a first-class citizen

πŸš€ Want to build your own AI agent?

In 90 minutes, learn exactly how I built my AI agent team that handles 50,000 tasks per week β€” using the same system I run every day.

🎟️ Get the Crash Course β€” €49

One-time payment β€’ Lifetime access β€’ 14-day money-back guarantee

In Bash, there's trap for this:

cleanup() {
    rm -rf "$WORK_DIR"
}
trap cleanup EXIT

trap cleanup EXIT ensures that the cleanup() function always runs when the script exits β€” whether due to an error, a Ctrl+C, or normal completion.

One line of code. That would have saved 36 GB.

The same logic in Python

In Python, use context managers or try/finally:

import tempfile, shutil

with tempfile.TemporaryDirectory() as tmpdir:
    # Everything inside this block
    # tmpdir is automatically deleted β€” even on errors
    download_data(tmpdir)
    process_data(tmpdir)

Or explicitly:

try:
    download_and_process()
finally:
    shutil.rmtree(work_dir, ignore_errors=True)

What I'm taking from this

When AI agents handle tasks automatically, a new class of bugs emerges. Not because the AI works incorrectly, but because automated processes run in the background β€” without anyone checking the server periodically.

Three rules I'm now enforcing for all automated scripts:

  • Always trap. Every script that creates temporary files must also clean them up β€” on success AND on failure.
  • Monitor disk usage. A simple cron job that sends an alert above 85% would have surfaced this problem two weeks earlier.
  • Name temp directories explicitly. /tmp/mastr-sync-2026-03-16 instead of /tmp/mastr-sync β€” so you can immediately see how old they are.

The result

37 gigabytes freed. Script fixed. Wind energy platform back online.

And a blog post that hopefully prevents you from making the same mistake.

If you're curious how I use AI agents to build and monitor these automated processes, check out:

The simplest bugs are often the most expensive β€” because they build up over weeks before they're noticed.

πŸš€ Want to build your own AI agent?

In 90 minutes, learn exactly how I built my AI agent team that handles 50,000 tasks per week β€” using the same system I run every day.

🎟️ Get the Crash Course β€” €49

One-time payment β€’ Lifetime access β€’ 14-day money-back guarantee

Tags

AutomationAI AgentsDevOpsBuilding in PublicLessons Learned

About the Author

Jan Koch

Jan Koch

KI Experte, Berater und Entwickler. Ich helfe Unternehmern und Entwicklern, KI effektiv einzusetzen - von der Strategie bis zur Implementierung.

Every Tuesday

AI Made Simple

Get a short email every Tuesday with relevant AI examples for entrepreneurs, practical tips, and future insights.

1,000+ subscribers β€’ No spam β€’ Unsubscribe anytime