Skip to content

dipeshchaudhari3255/TempCleanupService

Repository files navigation

Temporary Files Cleanup Service

A Windows background service that automatically cleans temporary files and cache daily at 10:00 AM and when Windows starts.

Features

Automatic Cleanup - Runs daily at 10:00 AM ✅ Startup Cleanup - Cleans on Windows startup ✅ Admin Execution - Runs with LocalSystem privileges ✅ Comprehensive Cleaning - Removes:

  • User TEMP folder (%TEMP%)
  • User Local Temp (C:\Users\<User>\AppData\Local\Temp)
  • System TEMP (C:\Windows\Temp)
  • Recycle Bin (C:\$Recycle.Bin)
  • Crash Dumps (C:\Users\<User>\AppData\Local\CrashDumps)
  • Windows Error Reporting (C:\ProgramData\Microsoft\Windows\WER\)
  • Delivery Optimization Cache
  • Windows Update Cache

Error Handling - Gracefully handles locked files and permission issues ✅ Detailed Logging - Creates daily log files at C:\ProgramData\TempCleanupService\Safe Deletion - Skips files/folders that are in use

Requirements

  • Windows 7 or later
  • .NET 6.0 Runtime (or .NET Framework 4.7.2 if using Framework version)
  • Administrator privileges for installation and operation

Installation

Step 1: Build the Service

cd "d:\DC\My Projects\TempDeleted System"
dotnet publish -c Release

Or if using Visual Studio, build in Release configuration.

Step 2: Install the Service

  1. Open Command Prompt as Administrator
  2. Navigate to the project directory
  3. Run: install.bat

The script will:

  • Check administrator privileges
  • Create the Windows Service
  • Set startup type to Automatic
  • Start the service immediately

Step 3: Verify Installation

Check that the service is running:

sc query TempCleanupService

Status should show "RUNNING"

Logs

View cleanup activity logs at:

C:\ProgramData\TempCleanupService\CleanupLog_YYYY-MM-DD.txt

Each log file contains timestamped entries showing:

  • Cleanup start/end times
  • Directories cleaned
  • Files and space freed
  • Any errors encountered

Uninstallation

To remove the service:

  1. Open Command Prompt as Administrator
  2. Navigate to the project directory
  3. Run: uninstall.bat

The script will stop and remove the service. Log files are preserved.

Manual Service Management

Start the Service

net start TempCleanupService

Stop the Service

net stop TempCleanupService

Remove the Service

sc delete TempCleanupService

Check Service Status

sc query TempCleanupService

Architecture

TempCleanupService.cs

Main Windows Service class that:

  • Initializes on startup
  • Schedules daily cleanup at 10:00 AM
  • Manages timers and cleanup execution

CleanupWorker.cs

Handles the actual cleanup operations:

  • Iterates through configured directories
  • Deletes files and empty folders
  • Tracks statistics (files deleted, space freed)
  • Handles access denied errors gracefully

ProjectInstaller.cs

Windows Service installer that:

  • Defines service installation parameters
  • Sets service to run as LocalSystem
  • Configures automatic startup

Scheduling Details

  • On Startup: Service runs cleanup immediately when Windows starts
  • Daily: Cleanup runs at 10:00 AM every day
  • Check Interval: Service checks every 60 seconds if it's time to run

Security Considerations

  • Service runs as LocalSystem (highest Windows privileges)
  • Only accessible to administrators
  • Handles permission errors gracefully
  • Skips locked/in-use files
  • Creates detailed audit logs

Troubleshooting

Service won't start

  • Ensure .NET 6.0 Runtime is installed
  • Check that the executable path is correct
  • Verify administrator privileges
  • Review event logs: Event Viewer > Windows Logs > System

Cleanup not running at 10 AM

  • Verify service is running: sc query TempCleanupService
  • Check logs at C:\ProgramData\TempCleanupService\
  • Ensure system clock is correct

Access Denied errors in logs

  • This is normal for system files in use
  • Service automatically skips them
  • Critical system files are protected by Windows

Not freeing enough space

  • Some files may be locked by running applications
  • Restarting the system can help
  • Some protected system files cannot be deleted

Version History

v1.0.0 (2026-02-05)

  • Initial release
  • Daily scheduling at 10 AM
  • Startup cleanup
  • Comprehensive temp directory coverage

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors