PowerShell script to delete all Docker Desktop objects, supposedly
Warning
This simple script will delete all your Docker containers, images, volumes, & builds, including their caches, and then compact docker_data.vhdx automatically. So use at your own risk!
That's what I'd like to say... But even with all of these prune commands, there are some persistent builds that can't be deleted! In that case, you gotta remove them manually from the GUI: Builds > Build history. Select builds, then remove.
- Docker Desktop with WSL2
- Windows PowerShell
Tested with:
- Docker Desktop 4.46.0
- WSL2 2.6.1.0
- PowerShell 5.1 & 7.5.3
(🤔 hmm I gotta update this)
- Open PowerShell.
- Enter the following command:
powershell -ExecutionPolicy ByPass -File PATH\TO\THIS\SCRIPTReplace PATH\TO\THIS\SCRIPT with its actual path. For example,
C:\Users\mayonnaisu\Download\docker-purge-main\docker-purge.ps1
- Select "Yes" when UAC prompt pops up.
- Open PowerShell as Administrator.
- Change PowerShell execution policy:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine- Enter y or yes.
Note
You don't need to do step 1-3 again the next time you run this script unless you have changed the execution policy to "Restricted" again.
- Right click on this script.
- Select "Run with PowerShell".
- Select "Yes" when UAC prompt pops up.
You need to regularly compact the .vhdx file, used by Docker Desktop to store its data. It's because while it can expand automatically, it can't shrink automatically (as of now) even after you have removed a lot of data from the virtual disk.