Main Content

cancel

Cancel cleanup tasks

Since R2025a

    Description

    cancel(cleanupObj) cancels any cleanup tasks specified by the onCleanup object and prevents them from being executed when cleanupObj is destroyed.

    example

    Examples

    collapse all

    Create a function that includes an onCleanup object that displays a message when the object is destroyed but cancels displaying the message when the function executes successfully.

    function exampleFun
        cleanupObj = onCleanup(@()myCleanupFun);
        for n=1:5
            pause(1)
            disp(".")
        end
        cleanupObj.cancel
        disp("Function completed successfully")
    end
    
    function myCleanupFun
        disp("Function was canceled prematurely")
    end

    Run exampleFun.

    exampleFun
    .
    .
    .
    .
    .
    Function completed successfully

    Run exampleFun again, this time cancel execution while the function is running using Ctrl+C or Ctrl+Break. On Apple Macintosh platforms, you also can use Command+. (the Command key and the period key).

    exampleFun
    .
    .
    .
    
    Function was canceled prematurely
    Operation terminated by user during exampleFun (line 4)

    Input Arguments

    collapse all

    Cleanup object, specified as an onCleanup object.

    Version History

    Introduced in R2025a