scripts: cros_sdk: Delete out-path in --delete

We rely on cros_sdk_lib.CleanupChrootMount() to delete the chroot (the
"mount" part is mostly obsolete). "The chroot" consists of both the
chroot path and the output path these days, so we should probably be
deleting both. Otherwise, we run into unexpected cases around stale
contents in 'out-path' affecting a "newly" created chroot.

Supply a --no-delete-out-dir argument, to opt out of the new behavior
while keeping the behavior consistent with existing expecations (e.g.,
SdkService.Create(replace=True)).

BUG=b:265885353, b:297068910
TEST=./run_tests
TEST=cros_sdk --delete

Change-Id: Ie7bdc991fa887256b5607a791eb93d19b6b3f29f
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/chromite/+/4803934
Reviewed-by: Mike Frysinger <vapier@chromium.org>
Commit-Queue: Brian Norris <briannorris@chromium.org>
Tested-by: Brian Norris <briannorris@chromium.org>
diff --git a/scripts/cros_sdk.py b/scripts/cros_sdk.py
index 15d9e05..77e3a94 100644
--- a/scripts/cros_sdk.py
+++ b/scripts/cros_sdk.py
@@ -524,6 +524,14 @@
         help="Skip automatic SDK and toolchain upgrade when entering the "
         "chroot. Never guaranteed to work, especially as ToT moves forward.",
     )
+    parser.add_bool_argument(
+        "--delete-out-dir",
+        default=True,
+        enabled_desc="Delete the SDK build state along with the chroot. "
+        "Applies to --delete or --replace.",
+        disabled_desc="Don't delete the SDK build state along with the chroot. "
+        "Applies to --delete or --replace.",
+    )
 
     # Use type=str instead of type='path' to prevent the given path from being
     # transferred to absolute path automatically.
@@ -576,7 +584,7 @@
         "--delete",
         action="store_true",
         default=False,
-        help="Delete the current SDK chroot if it exists.",
+        help="Delete the current SDK chroot and build state if they exist.",
     )
     group.add_argument(
         "--force",
@@ -801,7 +809,9 @@
                         "cros_sdk was invoked with force option, continuing."
                     )
             logging.notice("Deleting chroot.")
-            cros_sdk_lib.CleanupChrootMount(chroot.path, delete=True)
+            cros_sdk_lib.CleanupChrootMount(
+                chroot, delete=True, delete_out=options.delete_out_dir
+            )
 
     # Enter a new set of namespaces.  Everything after here cannot directly
     # affect the hosts's mounts or alter LVM volumes.