A Python utility to scan and clean system temporary directories on Windows, Linux, and macOS.
It performs a safe dry run by default, shows which files would be deleted, tracks total size scanned, and asks for confirmation before actually removing anything. The script also logs cleanup activity and reports how much disk space was freed. It includes safety checks to prevent cleaning the root directory.
- Detects and cleans system temp folders on Windows, Linux, and macOS
- Recursively scans directories and tracks total size scanned
- Deletes files older than a configurable number of days
- Dry run mode by default (preview deletions safely)
- Concurrent file processing for faster cleanup
- Generates cleanup summary with counts and disk space freed
- Removes empty directories after cleanup
- Handles locked or inaccessible files gracefully
- Prevents accidental cleaning of root directories
- Logs all actions to
log.txt
By default, the script detects system temporary directories for your OS:
- Windows:
%TEMP%,%TMP%,C:\Windows\Temp - Linux:
/tmp,/var/tmp - macOS:
$TMPDIR,/tmp,/private/tmp
You can also provide custom folders with the --folder option.
- Python 3.10+
No external dependencies are required. The script uses only Python’s standard library.
Clone the repository:
git clone https://github.com/mdshakerullahS/temp-cleaner.gitMove into the project directory:
cd temp-cleanerRun the script:
python main.pyBy default, the script will:
- Detect system temp directories based on your OS
- Scan temp folders recursively
- Track total size scanned
- Find files older than 3 days
- Run in dry run mode
- Ask for confirmation before deleting anything
- Refuse to clean the root directory for safety
Delete files older than a specific number of days.
python main.py --days 7python main.py --autoThis deletes files immediately without asking for confirmation.
Provide additional folders to clean.
python main.py --folder "D:/temp"Multiple folders can be added:
python main.py --folder "D:/temp" --folder "E:/cache"Control the number of worker threads.
python main.py --workers 20Default value:
10
python main.py --days 5 --autoThis command will:
- Delete files older than 5 days
- Run cleanup without confirmation
--- Starting DRY RUN ---
Files scanned: 5241 (2.15GB)
[DRY RUN] Would delete: C:\Temp\a.tmp
[DRY RUN] Would delete: C:\Temp\b.log
Files older than 5 days: 245
Delete these files? (Y/N):
After cleanup:
--- CLEANUP STARTED 2026-03-10 10:12:34 ---
[DELETED] C:\Temp\a.tmp
[DELETED] C:\Temp\b.log
----- CLEANUP SUMMARY -----
Start Time : 2026-03-10 10:12:31
End Time : 2026-03-10 10:12:38
Duration : 0:00:07
Files Scanned : 5241 (2.15 GB)
Old Files : 245
Deleted : 240
Skipped : 3
Failed : 2
Freed Space : 1.43 GB
---------------------------
Cleanup activity is saved to:
log.txt
The log includes:
- Deleted files
- Skipped files
- Errors
- Cleanup summaries
- Detects system temporary directories based on the OS (Windows, Linux, macOS).
- Recursively scans directories using
os.scandir. - Calculates file age using the last modification timestamp.
- Tracks total size scanned and identifies old files.
- Uses
ThreadPoolExecutorfor concurrent file processing. - Performs deletion after confirmation (unless
--autois used). - Logs results and prints a cleanup summary.
- Removes empty directories after files are deleted.
- Skips cleaning the root directory to avoid catastrophic deletion.
This project is licensed under the MIT License.
You are free to use, modify, and distribute this software with proper attribution.
Md Shakerullah Sourov
Full-Stack Web Developer
- Portfolio: https://shakerullah.vercel.app
- LinkedIn: https://linkedin.com/in/mdshakerullah
- Email: sourovmdshakerullah@gmail.com
Temporary folders can silently grow to several gigabytes over time. This script provides a safe and simple way to periodically clean them while still allowing you to preview changes before deleting anything.