一个每天开机自动删除微信聊天记录的插件
在公司的电脑上不想留存自己的个人信息。万一公司有人半夜打开你的电脑,想想就可怕...
背景故事
作为一名在科技公司工作的程序员,我每天都会在公司电脑上使用微信进行工作沟通。但有时候也会处理一些私人聊天,这些聊天记录如果被同事或IT部门看到,后果不堪设想。
特别是想到:
- 加班到深夜,忘记退出微信
- 同事借用电脑处理紧急事务
- IT部门进行系统维护时可能查看文件
为了保护个人隐私,我开发了一个PowerShell脚本,能够在每次开机时自动清理微信聊天记录。
解决方案
核心功能
- ✅ 自动运行:系统启动时自动执行
- ✅ 智能检测:如果微信正在运行则跳过清理
- ✅ 彻底清理:删除聊天记录、缓存文件、附件等
- ✅ 空间释放:清理不必要的微信数据,释放磁盘空间
- ✅ 易于管理:可随时启用或禁用自动清理
清理内容
- 聊天记录数据库文件(.db, .db-shm, .db-wal)
- 消息附件和缓存文件
- 图片、视频、文件缓存
- 联系人数据库
- 表情包数据
代码实现
1. 主清理脚本(clean_wechat_e_drive_english.ps1)
# WeChat Chat History Cleaner
# Function: Safely delete WeChat chat history data
# Warning: This operation is irreversible, use with caution
param(
[switch]$Backup = $false, # Whether to create backup
[switch]$DryRun = $false # Whether to only show files without actual deletion
)
# WeChat data storage paths
$WeChatPaths = @(
"C:\\Users\\DELL\\Documents\\xwechat_files", # Custom WeChat files location
"$env:USERPROFILE\\Documents\\WeChat Files", # WeChat files in user documents
"$env:APPDATA\\Tencent\\WeChat", # WeChat data in AppData
"$env:LOCALAPPDATA\\Tencent\\WeChat" # WeChat data in LocalAppData
)
# Files and folders patterns to clean
$CleanPatterns = @(
"*.db", # Database files
"*.db-shm", # SQLite shared memory files
"*.db-wal", # SQLite write-ahead logs
"Msg\\*", # Message folder
"FileStorage\\*", # All file storage
"Backup\\*", # Backup files
"Temp\\*", # Temporary files
"Cache\\*", # Cache files
"*Msg*", # Message related files
"*Chat*", # Chat related files
"*History*" # History related files
)
function Write-ColorOutput {
param([string]$Message, [string]$Color = "White")
Write-Host $Message -ForegroundColor $Color
}
function Test-WeChatRunning {
$process = Get-Process -Name "WeChat" -ErrorAction SilentlyContinue
if ($process -ne $null) {
Write-ColorOutput "WeChat process detected: $($process.Id)" "Red"
return $true
}
return $false
}
function Clean-WeChatData {
param([string]$BasePath)
if (-not (Test-Path $BasePath)) {
Write-ColorOutput "Path not found: $BasePath" "Yellow"
return 0, 0
}
Write-ColorOutput "Scanning: $BasePath" "Cyan"
$totalSize = 0
$fileCount = 0
# Calculate total size first
$originalSize = Get-WeChatDataSize -Path $BasePath
$originalSizeMB = [math]::Round($originalSize / 1MB, 2)
Write-ColorOutput "Current data size: $originalSizeMB MB" "Cyan"
# Three cleaning strategies
# 1. Folder pattern matching
# 2. File pattern matching
# 3. Recursive search for WeChat related files
# ... (完整的清理逻辑)
return $fileCount, $totalSize
}
# Main program
Write-ColorOutput "=== WeChat Chat History Cleaner ===" "Magenta"
if (Test-WeChatRunning) {
Write-ColorOutput "WARNING: WeChat is running!" "Red"
Write-ColorOutput "Please close WeChat first, then run this script again." "Yellow"
exit 1
}
# Display paths to be cleaned
Write-ColorOutput "WeChat data paths to scan:" "Cyan"
$totalOriginalSize = 0
foreach ($path in $WeChatPaths) {
if (Test-Path $path) {
$size = Get-WeChatDataSize -Path $path
$sizeMB = [math]::Round($size / 1MB, 2)
Write-ColorOutput " ✓ $path ($sizeMB MB)" "Green"
$totalOriginalSize += $size
}
}
# Execute cleaning operation
Write-Host ""
Write-ColorOutput "Starting WeChat data cleaning..." "Cyan"
$totalFiles = 0
$totalFreed = 0
foreach ($path in $WeChatPaths) {
$files, $size = Clean-WeChatData -BasePath $path
$totalFiles += $files
$totalFreed += $size
}
# Display results
Write-ColorOutput "=== Cleaning Complete ===" "Magenta"
if ($totalFiles -gt 0) {
$freedMB = [math]::Round($totalFreed / 1MB, 2)
Write-ColorOutput "Files processed: $totalFiles" "White"
Write-ColorOutput "Space freed: $freedMB MB" "White"
}
2. 开机自启动脚本(auto_clean_wechat_on_startup.ps1)
# Auto WeChat Cleaner for Startup
# Function: Automatically clean WeChat chat history on system startup
function Create-StartupShortcut {
param([string]$ScriptPath)
$startupFolder = "$env:APPDATA\\Microsoft\\Windows\\Start Menu\\Programs\\Startup"
$shortcutPath = "$startupFolder\\WeChatCleaner.lnk"
Write-Host "Creating startup shortcut at: $shortcutPath"
try {
$shell = New-Object -ComObject WScript.Shell
$shortcut = $shell.CreateShortcut($shortcutPath)
$shortcut.TargetPath = "powershell.exe"
$shortcut.Arguments = '-ExecutionPolicy Bypass -File "' + $ScriptPath + '"'
$shortcut.WorkingDirectory = Split-Path -Parent $ScriptPath
$shortcut.Description = "Auto WeChat Chat History Cleaner"
$shortcut.Save()
Write-Host "Startup shortcut created successfully!"
return $true
}
catch {
Write-Host "Failed to create startup shortcut: $($_.Exception.Message)" -ForegroundColor Red
return $false
}
}
# Main setup script
Write-Host "=== Auto WeChat Cleaner Setup ==="
Write-Host "Date: $(Get-Date)"
Write-Host ""
$cleanerScriptPath = "$PSScriptRoot\\clean_wechat_e_drive_english.ps1"
if (-not (Test-Path $cleanerScriptPath)) {
Write-Host "Error: Main cleaner script not found" -ForegroundColor Red
exit 1
}
# Create startup shortcut
$shortcutCreated = Create-StartupShortcut -ScriptPath $cleanerScriptPath
if ($shortcutCreated) {
Write-Host ""
Write-Host "=== Setup Complete ===" -ForegroundColor Green
Write-Host "The WeChat cleaner will now run automatically on system startup."
}
使用方法
1. 首次设置
# 运行设置脚本
.\\auto_clean_wechat_on_startup.ps1
2. 测试清理效果
# 测试模式(不实际删除)
.\\clean_wechat_e_drive_english.ps1 -DryRun
# 带备份的清理
.\\clean_wechat_e_drive_english.ps1 -Backup
# 直接清理
.\\clean_wechat_e_drive_english.ps1
3. 校验启动项设置
# 查看启动文件夹中的快捷方式
Get-ChildItem "$env:APPDATA\\Microsoft\\Windows\\Start Menu\\Programs\\Startup"
# 查看所有启动项
Get-CimInstance Win32_StartupCommand | Where-Object {$_.Command -like "*WeChatCleaner*"}
# 检查注册表启动项
Get-ItemProperty "HKCU:\\Software\\Microsoft\\Windows\\CurrentVersion\\Run"
4. 移除自动清理
# 删除启动快捷方式
Remove-Item "$env:APPDATA\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\WeChatCleaner.lnk"
# 或者直接打开启动文件夹手动删除
Start-Process "shell:startup"
技术特点
安全性设计
- 进程检测:如果微信正在运行,自动跳过清理避免数据损坏
- 错误处理:完善的异常捕获和错误提示
- 权限控制:只在用户级别运行,不影响系统其他用户
智能清理策略
- 文件夹模式匹配:删除特定的微信数据文件夹
- 文件模式匹配:删除根目录下的微信相关文件
- 递归搜索:深度搜索所有包含关键词的文件
用户体验
- 彩色输出:易于阅读的执行状态提示
- 进度显示:实时显示清理进度和释放空间
- 详细日志:记录每个操作步骤和结果
实际效果
在我的测试环境中,这个插件能够:
- 每次开机自动清理约150-200MB的微信数据
- 彻底删除聊天记录、文件缓存等敏感信息
- 不影响微信的正常使用(清理后重启微信即可)
- 系统启动时几乎无感知地完成清理
注意事项
- 数据不可恢复:清理操作不可逆,重要数据请提前备份
- 微信需关闭:运行清理前确保微信完全退出
- 首次使用测试:建议先用测试模式验证效果
- 定期检查:建议每月检查一次启动项是否正常
总结
这个自动清理插件完美解决了我在公司电脑上的隐私担忧。现在即使忘记退出微信,或者同事借用电脑,我也不用担心个人聊天记录被看到。
代码开源,欢迎大家根据自己需求进行修改和优化。保护个人隐私,从每天自动清理微信记录开始!
本文代码已在Windows 10/11环境下测试通过,适用于个人隐私保护场景。