CI: Defer unused cache directory purge to end of job
diff --git a/ci/scripts/build.linux.script.sh b/ci/scripts/build.linux.script.sh
index 352c3d6..23cef26 100755
--- a/ci/scripts/build.linux.script.sh
+++ b/ci/scripts/build.linux.script.sh
@@ -78,9 +78,6 @@
       compiler_launcher="sccache"
       "${sccache_bin}" --zero-stats >/dev/null 2>&1 || true
       launchers="-DCMAKE_C_COMPILER_LAUNCHER=${sccache_bin} -DCMAKE_CXX_COMPILER_LAUNCHER=${sccache_bin}"
-      # Incompatible cache formats: purge restored .ccache/ so the uploaded cache
-      # archive only holds .sccache/ and stays strictly below the 5 GB runner cap.
-      rm -rf "${rootdir}/.ccache"
     else
       echo "Notice: sccache server failed to start (check GCS credentials/network); falling back to ccache."
       [[ -n "${sccache_cred_server_pid}" ]] && kill "${sccache_cred_server_pid}" 2>/dev/null || true
@@ -89,7 +86,6 @@
 
   if [[ -z "${compiler_launcher}" && -n "${ccache_bin}" ]]; then
     compiler_launcher="ccache"
-    rm -rf "${rootdir}/.sccache"
     launchers="-DCMAKE_C_COMPILER_LAUNCHER=${ccache_bin} -DCMAKE_CXX_COMPILER_LAUNCHER=${ccache_bin}"
     # Log stats per job via CCACHE_STATSLOG rather than global --zero-stats /
     # --show-stats so concurrent jobs sharing a host CCACHE_DIR do not reset or
@@ -109,6 +105,9 @@
     "${sccache_bin}" --show-stats 2>&1 || true
     "${sccache_bin}" --stop-server >/dev/null 2>&1 || true
     [[ -n "${sccache_cred_server_pid}" ]] && kill "${sccache_cred_server_pid}" 2>/dev/null || true
+    # Incompatible cache formats: purge unused .ccache/ before cache upload so the
+    # uploaded cache archive only holds .sccache/ and stays strictly below the 5 GB runner cap.
+    rm -rf "${rootdir}/.ccache"
   elif [[ "${compiler_launcher}" == "ccache" && -n "${ccache_bin}" ]]; then
     if [[ -n "${CCACHE_STATSLOG}" ]]; then
       "${ccache_bin}" --show-log-stats
@@ -116,6 +115,8 @@
     else
       "${ccache_bin}" --show-stats
     fi
+    # Incompatible cache formats: purge unused .sccache/ before cache upload.
+    rm -rf "${rootdir}/.sccache"
   fi
 }
 
diff --git a/ci/scripts/build.windows.script.ps1 b/ci/scripts/build.windows.script.ps1
index d5924d5..cd92319 100644
--- a/ci/scripts/build.windows.script.ps1
+++ b/ci/scripts/build.windows.script.ps1
@@ -137,10 +137,6 @@
       $sccache_cmake = $sccache_exe -replace '\\', '/'
       $launchers = "-DCMAKE_C_COMPILER_LAUNCHER=${sccache_cmake}",
                    "-DCMAKE_CXX_COMPILER_LAUNCHER=${sccache_cmake}"
-      # Incompatible cache formats: purge restored .ccache\ so the uploaded cache
-      # archive only holds .sccache\ and stays strictly below the 5 GB runner cap.
-      $old_ccache = Join-Path $rootdir ".ccache"
-      if (Test-Path $old_ccache) { Remove-Item -Recurse -Force $old_ccache }
     } else {
       Write-Warning "sccache server failed to start (check GCS credentials/network); falling back to ccache."
       if ($cred_server_job) {
@@ -153,8 +149,6 @@
   # 2. Fall back to ccache if sccache is unavailable or failed to start
   if ((-not $compiler_launcher) -and $ccache_exe) {
     $compiler_launcher = "ccache"
-    $old_sccache = Join-Path $rootdir ".sccache"
-    if (Test-Path $old_sccache) { Remove-Item -Recurse -Force $old_sccache }
     # EIGEN_CI_CCACHE_* provide the YAML fallback defaults.  A runner may explicitly
     # set standard CCACHE_* variables (e.g. to a persistent host directory) in
     # config.toml without being overridden by the YAML template.
@@ -256,11 +250,18 @@
     Stop-Job $cred_server_job -ErrorAction SilentlyContinue
     Remove-Job $cred_server_job -ErrorAction SilentlyContinue
   }
+  # Incompatible cache formats: purge unused .ccache\ before cache upload so the
+  # uploaded cache archive only holds .sccache\ and stays strictly below the 5 GB runner cap.
+  $old_ccache = Join-Path $rootdir ".ccache"
+  if (Test-Path $old_ccache) { Remove-Item -Recurse -Force $old_ccache }
 } elseif ($compiler_launcher -eq "ccache" -and $ccache_exe) {
   & $ccache_exe --show-log-stats
   if (Test-Path $env:CCACHE_STATSLOG) {
     Remove-Item $env:CCACHE_STATSLOG -Force
   }
+  # Incompatible cache formats: purge unused .sccache\ before cache upload.
+  $old_sccache = Join-Path $rootdir ".sccache"
+  if (Test-Path $old_sccache) { Remove-Item -Recurse -Force $old_sccache }
 }
 
 # Return to root directory.