Skip to main content

Uninstall, Reset, and Clean Reinstall

At some point you may need to remove a broken Docker installation, clear out corrupted state, or rebuild a host from scratch. This lesson covers how to do that safely -- without accidentally destroying data you still need.

Destructive Operations

The commands in this lesson can permanently delete containers, images, volumes, and configuration. Always back up first.

Know What You Can Lose

Before running any removal commands, understand what Docker stores on the host:

AssetLocationRisk If Lost
Container data/var/lib/docker/containers/Running state and logs gone
Images/var/lib/docker/image/Must re-pull or rebuild everything
Named volumes/var/lib/docker/volumes/Persistent application data lost (databases, uploads)
Compose filesYour project directoriesCannot recreate services without them
.env filesYour project directoriesRuntime secrets and configuration lost
daemon.json/etc/docker/daemon.jsonCustom daemon settings lost

The most critical items are named volumes (they contain your database data, file uploads, etc.) and compose files (they define how your services run).

Step 1: Inventory What You Have

Before touching anything, capture the current state:

# Running and stopped containers
docker ps -a

# Images
docker images

# Volumes (this is where persistent data lives)
docker volume ls

# Networks
docker network ls

# Compose projects
docker compose ls

# Disk usage summary
docker system df -v

Save this output somewhere outside the Docker host.

Step 2: Back Up Critical Data

Back Up Compose and Config Files

Copy your compose files, environment files, and any referenced configuration to a safe location:

# Example: back up a project directory
cp -r /opt/myapp /tmp/myapp-backup

# Or specifically
cp compose.yaml compose.yaml.backup
cp .env .env.backup

Back Up Named Volumes

Named volumes contain persistent data (databases, uploads, etc.). Use a temporary container to create a tar archive of each volume you need to keep:

docker run --rm \
-v my_volume:/source:ro \
-v "$PWD":/backup \
alpine:3.20 tar czf /backup/my_volume_backup.tar.gz -C /source .

Repeat for each critical volume. Verify the backup is not empty:

tar tzf my_volume_backup.tar.gz | head

Back Up Daemon Configuration

sudo cp /etc/docker/daemon.json /tmp/daemon.json.backup

Step 3: Stop All Workloads

Before uninstalling, stop everything gracefully:

# Stop all compose projects
docker compose down

# If you have multiple projects, stop each one
cd /opt/project-a && docker compose down
cd /opt/project-b && docker compose down

# Verify everything is stopped
docker ps -a

Step 4: Uninstall Docker Packages

On Ubuntu / Debian

sudo apt-get purge -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extras

# Remove the repository configuration
sudo rm /etc/apt/sources.list.d/docker.list
sudo rm /etc/apt/keyrings/docker.asc

On RHEL / CentOS / AlmaLinux

sudo yum remove -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

At this point, Docker packages are removed but your data is still on disk in /var/lib/docker.

Step 5: Decide -- Keep Data or Full Reset

Option A: Keep Data (for Reinstall)

If you plan to reinstall Docker and want to keep your existing images, containers, and volumes:

# Do nothing -- leave /var/lib/docker in place
# When you reinstall Docker, it will find the existing data

Option B: Full Reset (Clean Slate)

If you want to start completely fresh, remove all Docker data:

# Remove all Docker data (images, containers, volumes, networks)
sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd

# Remove daemon configuration
sudo rm -rf /etc/docker
Point of No Return

Deleting /var/lib/docker permanently destroys all images, containers, volumes, and their data. If you have not backed up your named volumes, that data is gone forever.

Step 6: Clean Reinstall

Follow the installation steps from Install Docker Engine on Linux to set up Docker again.

After reinstalling:

# Verify Docker is working
docker version
docker run --rm hello-world

# Restore daemon configuration
sudo cp /tmp/daemon.json.backup /etc/docker/daemon.json
sudo systemctl restart docker

# Restore compose files
cp -r /tmp/myapp-backup /opt/myapp

Restore Volumes

Recreate each volume and extract the backup:

docker volume create my_volume

docker run --rm \
-v my_volume:/target \
-v "$PWD":/backup \
alpine:3.20 sh -c "cd /target && tar xzf /backup/my_volume_backup.tar.gz"

Start Services and Verify

cd /opt/myapp
docker compose up -d
docker compose ps
docker compose logs --tail=20

Confirm your application is running and data is intact.

Quick Decision Guide

SituationRecommended Approach
Broken packages but data is fineUninstall packages, reinstall, keep /var/lib/docker
Corrupt daemon stateBack up volumes, full reset, reinstall, restore volumes
Migrating to a new serverBack up volumes and compose files, install fresh on new host, restore
Lab or test cleanupFull reset is fine after confirming nothing important is stored

Key Takeaways

  • Always inventory and back up before uninstalling anything.
  • Named volumes are the most critical data -- they contain databases, uploads, and application state.
  • Removing Docker packages does not delete /var/lib/docker -- you must do that explicitly for a full reset.
  • Deleting /var/lib/docker is irreversible. Verify your backups before running that command.
  • After a clean reinstall, restore daemon.json, compose files, and volume data in that order.

What's Next