Created
September 6, 2026 04:36
-
-
Save rendicahya/41207206173c3f639afa92e8068dddbf to your computer and use it in GitHub Desktop.
WhatsApp Cache Cleanup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ============================================ | |
| # WhatsApp Cache Cleanup | |
| # ============================================ | |
| # Relaunch in PowerShell when double-clicked | |
| if ($Host.Name -notlike "*ConsoleHost*") { | |
| Start-Process powershell.exe -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" | |
| exit | |
| } | |
| Clear-Host | |
| Write-Host "========================================" -ForegroundColor Cyan | |
| Write-Host " WhatsApp Cache Cleanup" -ForegroundColor Cyan | |
| Write-Host "========================================" -ForegroundColor Cyan | |
| Write-Host "" | |
| # -------------------------------------------- | |
| # Get number of days | |
| # -------------------------------------------- | |
| do { | |
| $DaysInput = Read-Host "Delete files older than how many days?" | |
| [int]$Days = 0 | |
| $ValidInput = [int]::TryParse($DaysInput, [ref]$Days) | |
| if (-not $ValidInput -or $Days -lt 0) { | |
| Write-Host "Please enter a valid number (0 or greater)." -ForegroundColor Red | |
| Write-Host "" | |
| } | |
| } while (-not $ValidInput -or $Days -lt 0) | |
| # -------------------------------------------- | |
| # Calculate cutoff date | |
| # -------------------------------------------- | |
| $CutoffDate = (Get-Date).AddDays(-$Days) | |
| $Folder = "$env:LOCALAPPDATA\Packages\5319275A.WhatsAppDesktop_cv1g1gvanyjgm\LocalCache" | |
| Write-Host "" | |
| Write-Host "Cutoff date : $($CutoffDate.ToString('dd/MM/yyyy HH:mm:ss'))" | |
| Write-Host "Target : $Folder" | |
| Write-Host "" | |
| # -------------------------------------------- | |
| # Check folder | |
| # -------------------------------------------- | |
| if (-not (Test-Path $Folder)) { | |
| Write-Host "WhatsApp folder not found." -ForegroundColor Yellow | |
| Write-Host "" | |
| Write-Host "Press any key to exit..." | |
| $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") | Out-Null | |
| exit | |
| } | |
| # -------------------------------------------- | |
| # Scan files | |
| # -------------------------------------------- | |
| Write-Host "Scanning files..." -ForegroundColor Cyan | |
| Write-Host "" | |
| $FilesToDelete = @( | |
| Get-ChildItem $Folder -File -Recurse -ErrorAction SilentlyContinue | | |
| Where-Object { | |
| $_.LastWriteTime -lt $CutoffDate | |
| } | |
| ) | |
| # -------------------------------------------- | |
| # Calculate total size | |
| # -------------------------------------------- | |
| $TotalBytes = ($FilesToDelete | Measure-Object -Property Length -Sum).Sum | |
| if ($null -eq $TotalBytes) { | |
| $TotalBytes = 0 | |
| } | |
| $TotalGB = [math]::Round($TotalBytes / 1GB, 2) | |
| # -------------------------------------------- | |
| # No files found | |
| # -------------------------------------------- | |
| if ($FilesToDelete.Count -eq 0) { | |
| Write-Host "No files older than $Days days were found." -ForegroundColor Green | |
| Write-Host "Space to free : 0 GB" | |
| Write-Host "" | |
| Write-Host "Press any key to exit..." | |
| $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") | Out-Null | |
| exit | |
| } | |
| # -------------------------------------------- | |
| # Show summary | |
| # -------------------------------------------- | |
| Write-Host "Files found : $($FilesToDelete.Count)" | |
| Write-Host "Space to free : $TotalGB GB" | |
| Write-Host "" | |
| # -------------------------------------------- | |
| # Confirmation | |
| # -------------------------------------------- | |
| do { | |
| $Confirmation = Read-Host "Delete these files? (Y/N)" | |
| $Confirmation = $Confirmation.ToUpper() | |
| if ($Confirmation -notin @("Y", "N")) { | |
| Write-Host "Please enter Y or N." -ForegroundColor Yellow | |
| } | |
| } while ($Confirmation -notin @("Y", "N")) | |
| if ($Confirmation -eq "N") { | |
| Write-Host "" | |
| Write-Host "Cleanup cancelled." -ForegroundColor Yellow | |
| Write-Host "" | |
| Write-Host "Press any key to exit..." | |
| $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") | Out-Null | |
| exit | |
| } | |
| # -------------------------------------------- | |
| # Delete files | |
| # -------------------------------------------- | |
| Write-Host "" | |
| Write-Host "Deleting files..." -ForegroundColor Cyan | |
| Write-Host "" | |
| $DeletedCount = 0 | |
| $FailedCount = 0 | |
| $DeletedBytes = 0 | |
| $Current = 0 | |
| $TotalFiles = $FilesToDelete.Count | |
| foreach ($File in $FilesToDelete) { | |
| $Current++ | |
| Write-Progress ` | |
| -Activity "Deleting WhatsApp cache files" ` | |
| -Status "$Current of $TotalFiles" ` | |
| -PercentComplete (($Current / $TotalFiles) * 100) ` | |
| -CurrentOperation $File.Name | |
| try { | |
| $FileSize = $File.Length | |
| Remove-Item $File.FullName -Force -ErrorAction Stop | |
| $DeletedCount++ | |
| $DeletedBytes += $FileSize | |
| } | |
| catch { | |
| $FailedCount++ | |
| } | |
| } | |
| Write-Progress ` | |
| -Activity "Deleting WhatsApp cache files" ` | |
| -Completed | |
| # -------------------------------------------- | |
| # Calculate actual freed space | |
| # -------------------------------------------- | |
| $DeletedGB = [math]::Round($DeletedBytes / 1GB, 2) | |
| # -------------------------------------------- | |
| # Results | |
| # -------------------------------------------- | |
| Write-Host "" | |
| Write-Host "========================================" -ForegroundColor Cyan | |
| Write-Host " Cleanup Result" -ForegroundColor Cyan | |
| Write-Host "========================================" -ForegroundColor Cyan | |
| Write-Host "" | |
| Write-Host "Files found : $TotalFiles" | |
| Write-Host "Files deleted : $DeletedCount" -ForegroundColor Green | |
| Write-Host "Files failed : $FailedCount" -ForegroundColor $(if ($FailedCount -gt 0) { "Yellow" } else { "Green" }) | |
| Write-Host "Space freed : $DeletedGB GB" -ForegroundColor Green | |
| Write-Host "" | |
| if ($FailedCount -gt 0) { | |
| Write-Host "Some files could not be deleted." -ForegroundColor Yellow | |
| Write-Host "They may be in use by WhatsApp or another process." | |
| } | |
| else { | |
| Write-Host "WhatsApp cleanup completed successfully." -ForegroundColor Green | |
| } | |
| Write-Host "" | |
| Write-Host "Press any key to exit..." | |
| $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") | Out-Null |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment