delete

Availability

Flash Player 5.

Usage

delete reference

Parameters

reference The name of the variable or object to eliminate.

Returns

A Boolean value.

Description

Operator; destroys the object or variable specified by the reference parameter, and returns true if the object was successfully deleted; otherwise returns a value of false. This operator is useful for freeing up memory used by scripts. Although delete is an operator, it is typically used as a statement, as in the following:

delete x;

The delete operator may fail and return false if the reference parameter does not exist, or may not be deleted. Predefined objects and properties, and variables declared with var, may not be deleted. You cannot use the delete operator to remove movie clips.

Example

Usage 1: The following example creates an object, uses it, and then deletes it after it is no longer needed.

account = new Object();
account.name = 'Jon';
account.balance = 10000;

delete account;

Usage 2: The following example deletes a property of an object.

// create the new object "account"
account = new Object();
// assign property name to the account 
account.name = 'Jon'; 
// delete the property
delete account.name; 

Usage 3: The following is another example of deleting an object property.

// create an Array object with length 0
my_array = new Array(); 
// add an element to the array. Array.length is now 1
my_array[0] = "abc";
// add another element to the array. Array.length is now 2
my_array[1] = "def"; 
// add another element to the array. Array.length is now 3
my_array[2] = "ghi";
// my_array[2] is deleted, but Array.length is not changed
delete array[2]; 
trace(my_array.length);

Usage 4: The following example illustrates the behavior of delete on object references.

// create a new object, and assign the variable ref1

// to refer to the object
ref1 = new Object();
ref1.name = "Jody";
// copy the reference variable into a new variable
// and delete ref1
ref2 = ref1;
delete ref1;

If ref1 had not been copied into ref2, the object would have been deleted when ref1 was deleted, because there would be no references to it. If you delete ref2, there will no longer be any references to the object; it will be destroyed, and the memory it was using will be made available.

See also

var