@@ -832,6 +832,19 @@ def run_janitor(self, ignore_ttl: bool) -> bool:
832832
833833 return success
834834
835+ @python_api_analytics
836+ def destroy (self ) -> bool :
837+ success = False
838+
839+ if self .console .start_destroy ():
840+ try :
841+ self ._destroy ()
842+ success = True
843+ finally :
844+ self .console .stop_destroy (success = success )
845+
846+ return success
847+
835848 @t .overload
836849 def get_model (
837850 self , model_or_snapshot : ModelOrSnapshot , raise_if_missing : Literal [True ] = True
@@ -1554,16 +1567,19 @@ def apply(
15541567 )
15551568
15561569 @python_api_analytics
1557- def invalidate_environment (self , name : str , sync : bool = False ) -> None :
1570+ def invalidate_environment (
1571+ self , name : str , sync : bool = False , protect_prod : t .Optional [bool ] = True
1572+ ) -> None :
15581573 """Invalidates the target environment by setting its expiration timestamp to now.
15591574
15601575 Args:
15611576 name: The name of the environment to invalidate.
15621577 sync: If True, the call blocks until the environment is deleted. Otherwise, the environment will
15631578 be deleted asynchronously by the janitor process.
1579+ protect_prod: If True, prevents invalidation of the production environment.
15641580 """
15651581 name = Environment .sanitize_name (name )
1566- self .state_sync .invalidate_environment (name )
1582+ self .state_sync .invalidate_environment (name , protect_prod )
15671583 if sync :
15681584 self ._cleanup_environments ()
15691585 self .console .log_success (f"Environment '{ name } ' deleted." )
@@ -2537,6 +2553,21 @@ def _context_diff(
25372553 gateway_managed_virtual_layer = self .gateway_managed_virtual_layer ,
25382554 )
25392555
2556+ def _destroy (self ) -> None :
2557+ # Invalidate all environments, including prod
2558+ for environment in self .state_reader .get_environments ():
2559+ self .invalidate_environment (name = environment .name , protect_prod = False )
2560+
2561+ # Run janitor to clean up all objects
2562+ self ._run_janitor (ignore_ttl = True )
2563+
2564+ # Remove state tables
2565+ self .state_sync .remove_state ()
2566+ self .console .log_status_update ("State tables removed." )
2567+
2568+ # Finally clear caches
2569+ self .clear_caches ()
2570+
25402571 def _run_janitor (self , ignore_ttl : bool = False ) -> None :
25412572 current_ts = now_timestamp ()
25422573
0 commit comments