Skip to content

Instantly share code, notes, and snippets.

@salehi
Created December 9, 2025 01:55
Show Gist options
  • Select an option

  • Save salehi/b52fea4017acb7b431e0b4f68fb82384 to your computer and use it in GitHub Desktop.

Select an option

Save salehi/b52fea4017acb7b431e0b4f68fb82384 to your computer and use it in GitHub Desktop.

K3s Disk Cleanup

Quick Commands

Images

# List images
k3s crictl images

# Remove unused images
k3s crictl rmi --prune

# Remove specific image
k3s crictl rmi <image-id>

Containers

# List all containers (including stopped)
k3s crictl ps -a

# Remove stopped containers
k3s crictl rm $(k3s crictl ps -a -q --state exited)

Pods

# List pods
k3s crictl pods

# Remove stopped pods
k3s crictl rmp $(k3s crictl pods -q --state ready=false)

Full Cleanup

# Remove all unused containers, images, and cache
k3s crictl rmi --prune

Logs

# Check log sizes
du -sh /var/log/containers/*
du -sh /var/log/pods/*

# Truncate large logs
truncate -s 0 /var/log/containers/*.log

K3s Data Directory

# Check k3s storage usage
du -sh /var/lib/rancher/k3s/*

# Containerd content store (largest usually)
du -sh /var/lib/rancher/k3s/agent/containerd/

Kubernetes-Level Cleanup

# Delete completed/failed pods
k3s kubectl delete pods --field-selector=status.phase==Succeeded -A
k3s kubectl delete pods --field-selector=status.phase==Failed -A

# Delete evicted pods
k3s kubectl get pods -A | grep Evicted | awk '{print $2 " -n " $1}' | xargs -L1 k3s kubectl delete pod

Automated Garbage Collection

K3s runs containerd GC automatically. To tune it, edit /etc/rancher/k3s/config.yaml:

kubelet-arg:
  - "image-gc-high-threshold=85"
  - "image-gc-low-threshold=80"

Restart after changes:

systemctl restart k3s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment