public void stripFields(Document target, FieldSet fieldSet) {
        List<Field> toStrip = new ArrayList<>();
        if (fieldSet instanceof DocumentOnly) {
            var actual = target.getDataType().fieldSet(DocumentOnly.NAME);
            if (actual != null) {
                for (Iterator<Map.Entry<Field, FieldValue>> i = target.iterator(); i.hasNext();) {
                    Map.Entry<Field, FieldValue> v = i.next();
                    if (! actual.contains(v.getKey())) {
                        toStrip.add(v.getKey());
                    }
                }
                for (Field f : toStrip) {
                    target.removeFieldValue(f);
                }
                return;
            }
        }
        for (Iterator<Map.Entry<Field, FieldValue>> i = target.iterator(); i.hasNext();) {
            Map.Entry<Field, FieldValue> v = i.next();

            if (!fieldSet.contains(v.getKey())) {
                toStrip.add(v.getKey());
            }
        }

        for (Field f : toStrip) {
            target.removeFieldValue(f);
        }
    }
Strips all fields not wanted by the given field set from the document.
document/src/main/java/com/yahoo/document/fieldset/FieldSetRepo.java