diff --git a/core/src/main/java/jenkins/model/Jenkins.java b/core/src/main/java/jenkins/model/Jenkins.java index b6303b377a9f..03c43b8bdfc4 100644 --- a/core/src/main/java/jenkins/model/Jenkins.java +++ b/core/src/main/java/jenkins/model/Jenkins.java @@ -2239,14 +2239,38 @@ public DescribableList, NodePropertyDescriptor> getGlobalNodePro * but we also call this periodically to self-heal any data out-of-sync issue. */ /*package*/ void trimLabels() { + trimLabels((Set) null); + } + + /** + * Reset labels and remove invalid ones for the given nodes. + * @param nodes the nodes taken as reference to update labels + */ + void trimLabels(Node... nodes) { + Set includedLabels = new HashSet<>(); + Arrays.asList(nodes).stream().filter(Objects::nonNull).forEach(n -> includedLabels.addAll(n.getAssignedLabels())); + trimLabels(includedLabels); + } + + /** + * Reset labels and remove invalid ones for the given nodes. + * @param includedLabels the labels taken as reference to update labels. If {@code null}, all labels are considered. + */ + private void trimLabels(@CheckForNull Set includedLabels) { Set