PrismLibrary/Prism · error · Exception

Cannot destroy .

Error message

Cannot destroy {view}.

What it means

Prism.Maui's MvvmHelpers.DestroyPage cleans up a page (view and ViewModel, children and modals) and wraps the whole operation in a try/catch; any exception during cleanup is rethrown as Exception 'Cannot destroy {view}.' with the original as InnerException. It indicates a failure while tearing down a page during navigation.

Solutions

  1. Inspect the InnerException to find the real failing component (ViewModel Destroy, OnNavigatedFrom, child cleanup).
  2. Wrap IDestructible.Destroy logic in the ViewModel with defensive null checks and its own try/catch.
  3. Ensure pages in the modal stack and their ViewModels implement cleanup safely and can handle repeated calls.
  4. Avoid throwing from IConfirmNavigationRequest/OnNavigatedFrom implementations.

Example fix

// before
public void Destroy()
{
    _subscription.Dispose(); // throws if already disposed
}
// after
public void Destroy()
{
    _subscription?.Dispose();
}
Defensive patterns

Strategy: try-catch

Type guard

bool SafeToDestroy(Page p) => p is not null && (p.BindingContext as IDestructible) is not null;

Try / catch

try { MvvmHelpers.DestroyPage(page); }
catch (Exception ex) when (ex.Message.StartsWith("Cannot destroy"))
{ Log(ex.InnerException); /* inspect real cause */ }

Prevention

When it happens

Trigger: Calling MvvmHelpers.DestroyPage (directly or via DestroyWithModalStack/HandleSystemGoBack) on a page whose OnNavigatedFrom/IConfirmNavigationRequest/IDestructible.Destroy throws, or whose modal stack or child pages fail to clean up.

Common situations: ViewModel's Destroy implementation throws (null dependencies, disposed resources); a page in the modal stack fails cleanup; navigation racing with page transition animations; exception inside a child view's cleanup during DestroyChildren.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of PrismLibrary/Prism@358118cd64 (2026-09-15). Data as JSON: /api/errors/430e7f1e13ce7921. Report an issue: GitHub.

Appendix: source

Thrown at src/Maui/Prism.Maui/Common/MvvmHelpers.cs:77

    {
        try
        {
            DestroyChildren(view);

            InvokeViewAndViewModelAction<IDestructible>(view, v => v.Destroy());

            //I'm actually not sure if this is necessary, but it seems like a good idea to clear the child regions of the page before we clear the behaviors and binding context of the page itself.
            if (view is Page page)
                page.ClearChildRegions();

            if (view is VisualElement visualElement)
            {
                DeferredCleanup(visualElement);
            }
        }
        catch (Exception ex)
        {
            throw new Exception($"Cannot destroy {view}.", ex);
        }
    }

    private static void DeferredCleanup(VisualElement visualElement)
    {
        // Delay cleanup until after page transition animations have completed.
        // We cannot clear BindingContext immediately because the page is still
        // visible during the pop animation, causing a visual flicker.
        // Neither Unloaded nor ParentChanged fire reliably across all platforms,
        // so a dispatcher delay is the only consistent approach.
        visualElement.Dispatcher?.DispatchDelayed(TimeSpan.FromMilliseconds(800), () =>
        {
            visualElement.Behaviors?.Clear();
            visualElement.BindingContext = null;
        });
    }

    private static void DestroyChildren(IView? page)

View on GitHub (pinned to 358118cd64)