You can delete a dynamic object with the DELETE OBJECT statement:

DELETE OBJECT handle.

You can also use the WIDGET keyword in place of OBJECT in this statement. The handle is a variable or temp-table field of type HANDLE, which was previously used in a CREATE statement to create the object. If there is no object currently associated with the handle, the AVM returns an error when it tries to execute the statement. In cases where your statement might be attempting to delete an object that has not been created or which has already been deleted, and you do not want to be informed of this at run time, you can avoid such an error by checking the handle in advance with the VALID-HANDLE function:

IF VALID-HANDLE(hButton) THEN
  DELETE OBJECT hButton.

Alternatively, you can include the NO-ERROR keyword on the DELETE statement to suppress any error message:

DELETE OBJECT hButton NO-ERROR.

In either case, do not forget the OBJECT keyword. If you do, the AVM thinks you are trying to delete a record from a table:

DEFINE VARIABLE hButton AS HANDLE NO-UNDO.
.
.
.
DELETE hButton. /* Do not do this! */

The following figure shows the result.

Figure 1. Unknown table error message

It is also important that you understand that you cannot delete a static object using its handle. Consider, for example, this sequence of statements:

DEFINE VARIABLE hButton AS HANDLE NO-UNDO.
DEFINE BUTTON MyButton.
DISPLAY MyButton.
hButton = MyButton:HANDLE.
DELETE OBJECT hButton.

If you run this procedure you get the error shown in the following figure at run time.

Figure 2. Static handle error message

The AVM deletes static objects for you when they go out of scope. You cannot delete them yourself.

Delete windows and frames

If you delete a dynamic frame, any dynamic objects that the frame contains are also deleted. If you delete a dynamic window, any frames that the window contains (both static and dynamic frames) are de-parented from the window and are unrealized. The frames remain valid, and you can re-parent them to another dynamic window.