7.1.6 Removing Nodes and Properties

Removing a node or property is done with the Item.remove method:

javax.jcr.
Item

void

remove()

Removes this item (and its subtree).

To persist a removal, a save must be performed that includes the (former) parent of the removed item within its scope.

A ReferentialIntegrityException will be thrown on save if this item or an item in its subtree is currently the target of a REFERENCE property located in this workspace but outside this item's subtree and the current Session has read access to that REFERENCE property.

An AccessDeniedException will be thrown on save if this item or an item in its subtree is currently the target of a REFERENCE property located in this workspace but outside this item's subtree and the current Session does not have read access to that REFERENCE property.

A ConstraintViolationException will be thrown either immediately (by this method), or on save, if removing this item would violate a node type or implementation-specific constraint. Implementations may differ on when this validation is performed.

A VersionException will be thrown either immediately (by this method), or on save, if the parent node of this item is versionable and checked-in or is non-versionable but its nearest versionable ancestor is checked-in. Implementations may differ on when this validation is performed.

A LockException will be thrown either immediately (by this method), or on save, if a lock prevents the removal of this item. Implementations may differ on when this validation is performed.

A RepositoryException is thrown if another error occurs.



A property can also be removed by setting its value to null. When this is done, the null parameter must be cast to a non-array type for single value properties and an array type for multi-value properties.

Note that setting a multi-value property to an array containing null values is different from setting it to a null cast to an array type. In the former case, all null values within the array are removed and the array is compacted to include only non-null values even if this results in a multi-value property being set to an empty array. In the latter case the entire property is removed. For example,

p.setValue((String[])null)

would remove property p, whereas

p.setValue(new String[]{"a", null, "b"}) would set p to ["a","b"]

and

p.setValue(new String[]{null}) would set p to the empty array, [].

See also 4.7.3 No Null Values.