Skip to content

Instantly share code, notes, and snippets.

@ponfertato
Created November 27, 2025 07:13
Show Gist options
  • Select an option

  • Save ponfertato/699c971b5651042761c134fa72a87854 to your computer and use it in GitHub Desktop.

Select an option

Save ponfertato/699c971b5651042761c134fa72a87854 to your computer and use it in GitHub Desktop.
This script safely clears AnyDesk configuration and traces on a single Windows PC so AnyDesk will generate a new device ID on next start. It performs admin checks, stops AnyDesk, backs up configs, removes user and service traces (logs, traces, config files), and restarts the client.
@echo off
chcp 65001 >nul
mode con: cols=80 lines=20
color 0a
setlocal enabledelayedexpansion
:: Simple log helper
set LOG=%~dp0anydesk_cleanup.log
for /f "tokens=1-3 delims=/:. " %%a in ("%date% %time%") do set TS=%%a-%%b-%%c_%%d
echo [%date% %time%] START >> "%LOG%"
:: Admin check
openfiles >nul 2>&1
if errorlevel 1 (
echo [%date% %time%] ERROR: Administrator privileges required. >> "%LOG%"
echo Please run this script as Administrator.
pause
exit /b 1
)
:: Detect architecture and install paths
if exist "%ProgramFiles(x86)%" (
set OSTYPE=64
set PDIR=%ProgramFiles(x86)%\AnyDesk
) else (
set OSTYPE=32
set PDIR=%ProgramFiles%\AnyDesk
)
echo [%date% %time%] Detected OS type: %OSTYPE% >> "%LOG%"
:: Function: terminate AnyDesk (wait up to 20s)
set WAIT_LIMIT=20
echo [%date% %time%] Attempting to terminate AnyDesk processes... >> "%LOG%"
for /f "tokens=2" %%P in ('tasklist /FI "IMAGENAME eq AnyDesk.exe" /NH 2^>nul ^| findstr /I "AnyDesk.exe"') do (
set PID=%%P
echo [%date% %time%] Killing PID !PID! >> "%LOG%"
taskkill /F /PID !PID! >nul 2>&1
)
set /a WAITED=0
:CHECK_LOOP
set PROC_EXISTS=
for /f "tokens=1" %%A in ('tasklist /FI "IMAGENAME eq AnyDesk.exe" /NH 2^>nul ^| findstr /I "AnyDesk.exe"') do set PROC_EXISTS=1
if defined PROC_EXISTS (
if %WAITED% GEQ %WAIT_LIMIT% (
echo [%date% %time%] WARNING: AnyDesk still running after %WAIT_LIMIT% seconds. >> "%LOG%"
) else (
timeout /t 1 >nul
set /a WAITED+=1
goto CHECK_LOOP
)
)
:: Locations to consider (config, traces, logs)
set PD=%ProgramData%\AnyDesk
set UD=%appdata%\AnyDesk
set ALLUSR=%allusersprofile%\AnyDesk
echo [%date% %time%] Config locations: PD=%PD% UD=%UD% ALLUSR=%ALLUSR% >> "%LOG%"
:: Backup and rename key config files if present
if exist "%PD%" (
pushd "%PD%"
if exist "service.conf" (
if not exist "service.conf.backup" (
copy /Y "service.conf" "service.conf.backup" >nul 2>&1 && echo [%date% %time%] Backed up service.conf >> "%LOG%"
) else echo [%date% %time%] service.conf.backup already exists >> "%LOG%"
) else echo [%date% %time%] service.conf not found in %PD% >> "%LOG%"
if exist "system.conf" (
if not exist "system.conf.backup" (
copy /Y "system.conf" "system.conf.backup" >nul 2>&1 && echo [%date% %time%] Backed up system.conf >> "%LOG%"
) else echo [%date% %time%] system.conf.backup already exists >> "%LOG%"
) else echo [%date% %time%] system.conf not found in %PD% >> "%LOG%"
popd
) else (
echo [%date% %time%] %PD% not found, skipping ProgramData backups >> "%LOG%"
)
:: Delete user AppData AnyDesk folder and common trace files
if exist "%UD%" (
echo [%date% %time%] Removing user AppData AnyDesk folder: %UD% >> "%LOG%"
rmdir /s /q "%UD%" 2>>"%LOG%" && echo [%date% %time%] Removed %UD% >> "%LOG%" || echo [%date% %time%] ERROR removing %UD% >> "%LOG%"
) else echo [%date% %time%] %UD% not present >> "%LOG%"
:: Remove common trace/log files in ProgramData (preserve .backup files)
if exist "%PD%" (
echo [%date% %time%] Cleaning %PD% files >> "%LOG%"
for /f "delims=" %%F in ('dir /b /a-d "%PD%"') do (
if /I not "%%F"=="service.conf.backup" if /I not "%%F"=="system.conf.backup" (
rem common trace/log filenames: *.trace, connection_trace.txt, ad*.trace, *.log
if /I "%%~xF"==".trace" ( del /f /q "%PD%\%%F" 2>>"%LOG%" )
if /I /c "%%F"=="connection_trace.txt" ( del /f /q "%PD%\%%F" 2>>"%LOG%" ) >nul 2>&1
if /I "%%~xF"==".log" ( del /f /q "%PD%\%%F" 2>>"%LOG%" )
)
)
) else echo [%date% %time%] %PD% not present >> "%LOG%"
:: Also try All Users ProgramData location for older Windows versions
if exist "%ALLUSR%" (
echo [%date% %time%] Cleaning %ALLUSR% >> "%LOG%"
rmdir /s /q "%ALLUSR%" 2>>"%LOG%" && echo [%date% %time%] Removed %ALLUSR% >> "%LOG%" || echo [%date% %time%] ERROR removing %ALLUSR% >> "%LOG%"
)
:: Rename remaining config files to .backup if present
if exist "%PD%\system.conf" if not exist "%PD%\system.conf.backup" ren "%PD%\system.conf" "system.conf.backup" 2>>"%LOG%" && echo [%date% %time%] Renamed system.conf >> "%LOG%"
if exist "%PD%\service.conf" if not exist "%PD%\service.conf.backup" ren "%PD%\service.conf" "service.conf.backup" 2>>"%LOG%" && echo [%date% %time%] Renamed service.conf >> "%LOG%"
:: Start AnyDesk if executable exists
if exist "%PDIR%\AnyDesk.exe" (
echo [%date% %time%] Starting AnyDesk from %PDIR% >> "%LOG%"
start "" "%PDIR%\AnyDesk.exe"
echo [%date% %time%] AnyDesk started >> "%LOG%"
) else (
echo [%date% %time%] AnyDesk executable not found at %PDIR% >> "%LOG%"
)
echo [%date% %time%] COMPLETED >> "%LOG%"
echo Operation completed. Log: %LOG%
pause
endlocal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment