Declarative Objectivity (DO) Language : Data Clauses : CLEAR Clause
CLEAR Clause
A CLEAR clause sets the value of one or more attributes or collection elements to null.
Syntax 
clearClause : CLEAR targetValue (',' targetValue)*;
targetValue : reachableValue ;
Used In 
UPDATE Statement
Quick Look 
Clear the username and password for a particular customer:
UPDATE Customer WHERE lastName == 'Mertz' CLEAR userId, pw;
Clear the list of rentals associated with a particular customer:
UPDATE Customer WHERE lastName == 'Mertz' CLEAR rentals;
Clear the postal code to a particular customer’s address:
UPDATE Customer WHERE lastName == 'Green' CLEAR address.postalCode;
Update a rental contract so that it no longer references an existing customer, and vice versa:
UPDATE RentalContract WHERE trackingNumber == 10 CLEAR customer;
Clear the first location in a rental company’s list of locations, and return the updated list:
UPDATE RentalCompany CLEAR locations[0] RETURN locations;
Clear the last 3 service records of a particular vehicle to a particular string:
UPDATE Vehicle WHERE license == 'ABC1997' CLEAR servRecs[-3:] RETURN servRecs;
Discussion 
A CLEAR clause occurs in an UPDATE Statement, and clears each specified target value by replacing it either with null or with a built-in default value, depending on the value type.
You specify the target values to be cleared using one or more comma-separated targetValue expressions. Each targetValue expression, in turn, evaluates to a single target value or to a sequence of target values within the current context object. A targetValue expression may return:
The value of a direct or indirect attribute of the current context object.
The values of one or more elements in a list or map that is directly or indirectly related to the current context object.
Note: You cannot clear the value of a set element; see Why CLEAR Doesn’t Clear Elements of a Set.
The values of a specified attribute within multiple elements of a collection that is directly or indirectly related to the current context object.
Clearing a target value replaces that value as shown in the following table:
Type of Target Value
Cleared Value
Displayed Value
Numeric
0
0
Boolean
False
false
String
Null, for variable-length string
null
Empty string, for fixed-length string
''
Date
Null
0001-01-01
Time
Null
00:00:00
Datetime
Null
0001-01-01 00:00:00
Reference
Null
0-0-0-0
Embedded object
Each component attribute is set to a value appropriate to the type
Collection
Null, for variable-size collection
null
Collection of null elements, for fixed-size collection
{}
Interval
Empty interval
{}
When targetValue evaluates to a sequence of multiple values, the CLEAR operation replaces each of those values with the same cleared value.
Clearing Bidirectional References
When a target value is a bidirectional reference to a persistent object, the CLEAR operation automatically updates the inverse within the referenced object.
For example, the example schema defines a bidirectional relationship between the RentalContract class and the Customer class through a pair of inverse reference attributes (namely, the RentalContract’s customer attribute and the the Customer’s rentals attribute). This enables a customer to have multiple rental contracts, where each rental contract belongs to exactly one customer.
Consider what happens when you clear the customer from a particular rental contract. This statement not only removes the Customer reference from the RentalContract’s customer attribute, but also automatically removes the RentalContract reference from the Customer’s rentals collection as well. In both cases, the removed references are replaced with the null reference 0-0-0-0:
UPDATE RentalContract WHERE trackingNumber == 10 CLEAR customer;
Clearing List Elements
When a target value is an individual element of a list, the CLEAR operation preserves the element, but replaces its value with an appropriate cleared value. You use a REMOVE Clause to completely remove an element of a list.
You can indicate an element you want to clear by specifying its position within the list:
Use a collection-element expression that specifies an index, to clear the element at that position. For example:
UPDATE RentalCompany CLEAR locations[0] RETURN locations;
Use a collection-element expression that specifies a range of indexes, to clear the element at each index position in the range. For example:
UPDATE Vehicle WHERE license == 'ABC1997' CLEAR servRecs[0:5] RETURN servRecs;
Note:A CLEAR operation affects only the value at an explicitly specified index position. If the list contains duplicate values at other positions, they are left unchanged. (This behavior differs from REMOVE, which affects all occurrences of a specified value in the list.)
If the elements of a list are referenced or embedded objects, you can indicate the ones you want to clear by using a collection-element expression that includes a predicate expression. For example:
UPDATE RentalCompany CLEAR locations[address.postalCode == '95128'];
Clearing Map Elements
Each element in a map is a key:value pair, where the key and the value are of types specified by the map. A CLEAR operation preserves the key within a key,:value pair, but replaces the associated value with an appropriate cleared value. You can use a REMOVE Clause to completely remove an entire key:value pair from a map.
You can indicate map elements to be cleared by qualifying their values with a predicate expression. For example, if the RentalCompany’s customers map has any elements whose values are references to Customers with the user identifier 'driverGuy', the following statement replaces those references with a null reference:
UPDATE RentalCompany CLEAR customers[userId == 'driverGuy'] RETURN customers;
Clearing Attribute Values Within Collected Objects
When the elements of a collection are referenced or embedded objects, you can use CLEAR to clear an attribute value within some or all of those objects. This can be a useful technique for resetting a particular attribute in many objects in a single batch operation. The collection of objects can be a list, set, or map.
You can clear a particular attribute value within every object in a collection by specifying a targetValue of the form collection.attribute, where collection evaluates to a collection that is reachable from the current context object, and attribute is an attribute of the collection element class. You can clear an attribute value in a subset of collected objects by using a collection-element expression to indicate that subset. For example:
The following statement iterates through the Vehicle objects in the RentalCompany’s vehicles collection, and clears the retired attribute of each Vehicle:
UPDATE RentalCompany CLEAR vehicles.retired RETURN vehicles;
The following statement uses a predicate expression to clear the availability of just the retired vehicles:
UPDATE RentalCompany CLEAR vehicles[retired == true].available RETURN vehicles;
The following statement specifies a range of indexes to clear the rate factor of just the first 25 locations:
UPDATE RentalCompany CLEAR locations[0:25].rateFactor RETURN locations;
Note: You can use index ranges only with lists, which maintain their elements in a predictable order.
Why CLEAR Doesn’t Clear Elements of a Set
You cannot use a CLEAR operation to clear an element of a set, because doing so would violate the integrity of the collection. A set organizes its elements based on their identity or values, and when a CLEAR operation clears an element, it does so without causing the set to perform any necessary reorganization. The effect would be to introduce a null element into the set that the set would not be able to find again. Consequently, the following statement, which attempts to clear the qualifying vehicle, returns an error because the vehicles collection of a RentalCompany is a set:
UPDATE RentalCompany CLEAR vehicles[license == 'ABC1997'] RETURN vehicles;
Use a REMOVE Clause to remove an element of a set.
Note:Although you cannot use CLEAR to directly clear a set element, you can use CLEAR to clear attribute values obtained from set elements.