- Reason for Creation
- Feature Overview
- Prerequisites
- Installation and Execution
- What It Does
- Limitations
- Output
I created this script because I often faced disk space issues on my Linux build agents.
Every time the disk filled up, I had to SSH into the agent manually, search for unused folders, and delete them — which was time-consuming and repetitive.
To automate this, I wrote a Python script and integrated it into my CI/CD build step, so the cleanup happens automatically without manual intervention.
A Python script that checks filesystem usage and automatically deletes a target directory when disk space usage exceeds 70%. It prints disk space before and after cleanup, and takes no action if usage is below the threshold.
- Checks filesystem usage on Linux.
- Deletes the specified directory only if usage exceeds 70%.
- Prints "Disk Space BEFORE Deletion" and "Disk Space AFTER Deletion".
- Skips deletion when usage is normal.
- Lightweight script suitable for CI/CD automation.
- Helps prevent server build failures due to insufficient disk space.
-
Python 3 Installed
If Python is not installed, download it from: https://www.python.org/downloads/ -
dzdo/sudo Access
The agent user must have dzdo privileges to run the script with elevated permissions. -
Linux Environment
This script is intended to run on a Linux build agent or server.
-
Navigate to the directory where
cleanup_disk_space.pyis located. -
Execute the script with dzdo and Python 3:
dzdo python3 cleanup_disk_space.py
-
Takes two variables:
filesystem→ filesystem path to checktargetdirectory→ directory to delete when usage is high
-
Calls
get_filesystem_usage(filesystem)to check the current disk usage. -
Prints Disk Space BEFORE Deletion.
-
If usage is greater than 70%:
- Deletes the folder inside
targetdirectory. - Calls
get_filesystem_usage(filesystem)again. - Prints Disk Space AFTER Deletion.
- Deletes the folder inside
-
If usage is 70% or below, no deletion is performed.
- The script supports only one filesystem and one target directory at a time.
- Multiple target directories cannot be cleaned in a single run.
- Deletion is permanent; ensure the
targetdirectoryis safe to remove. - Designed specifically for Linux environments; may not work on other OS.
Thank you for using this script!
Please ensure the targetdirectory is safe to delete before running.
Contributions and suggestions are always welcome!

