@@ -317,15 +317,29 @@ public boolean hasField(String path, boolean failOutOfRange) {
317317
318318 /**
319319 * Removes the field identified by the provided path.
320+ *
320321 * @param path the path of the field to be removed
321322 * @throws IllegalArgumentException if the path is null, empty, invalid or if the field doesn't exist.
322323 */
323324 public void removeField (String path ) {
325+ removeField (path , false );
326+ }
327+
328+ /**
329+ * Removes the field identified by the provided path.
330+ *
331+ * @param path the path of the field to be removed
332+ * @param ignoreMissing The flag to determine whether to throw an exception when `path` is not found in the document.
333+ * @throws IllegalArgumentException if the path is null, empty, or invalid; or if the field doesn't exist (and ignoreMissing is false).
334+ */
335+ public void removeField (String path , boolean ignoreMissing ) {
324336 final FieldPath fieldPath = FieldPath .of (path );
325337 Object context = fieldPath .initialContext (this );
326338 ResolveResult result = resolve (fieldPath .pathElements , fieldPath .pathElements .length - 1 , path , context );
327339 if (result .wasSuccessful ) {
328340 context = result .resolvedObject ;
341+ } else if (ignoreMissing ) {
342+ return ; // nothing was found, so there's nothing to remove :shrug:
329343 } else {
330344 throw new IllegalArgumentException (result .errorMessage );
331345 }
@@ -336,13 +350,13 @@ public void removeField(String path) {
336350 } else if (context instanceof IngestCtxMap map ) { // optimization: handle IngestCtxMap separately from Map
337351 if (map .containsKey (leafKey )) {
338352 map .remove (leafKey );
339- } else {
353+ } else if ( ignoreMissing == false ) {
340354 throw new IllegalArgumentException (Errors .notPresent (path , leafKey ));
341355 }
342356 } else if (context instanceof Map <?, ?> map ) {
343357 if (map .containsKey (leafKey )) {
344358 map .remove (leafKey );
345- } else {
359+ } else if ( ignoreMissing == false ) {
346360 throw new IllegalArgumentException (Errors .notPresent (path , leafKey ));
347361 }
348362 } else if (context instanceof List <?> list ) {
@@ -353,11 +367,13 @@ public void removeField(String path) {
353367 throw new IllegalArgumentException (Errors .notInteger (path , leafKey ), e );
354368 }
355369 if (index < 0 || index >= list .size ()) {
356- throw new IllegalArgumentException (Errors .outOfBounds (path , index , list .size ()));
370+ if (ignoreMissing == false ) {
371+ throw new IllegalArgumentException (Errors .outOfBounds (path , index , list .size ()));
372+ }
357373 } else {
358374 list .remove (index );
359375 }
360- } else {
376+ } else if ( ignoreMissing == false ) {
361377 throw new IllegalArgumentException (Errors .cannotRemove (path , leafKey , context ));
362378 }
363379 }
0 commit comments