void (Computable<? extends Collection<? extends Module>> toRemoveComputable, final @NotNull Collection<? extends DataNode<E>> toIgnore, @NotNull final ProjectData projectData, @NotNull final Project project, @NotNull final IdeModifiableModelsProvider modelsProvider) { final Collection<? extends Module> toRemove = toRemoveComputable.compute(); final List<Module> modules = new SmartList<>(toRemove); for (DataNode<E> moduleDataNode : toIgnore) { final Module module = modelsProvider.findIdeModule(moduleDataNode.getData()); ContainerUtil.addIfNotNull(modules, module); } if (modules.isEmpty()) { return; } ContainerUtil.removeDuplicates(modules); for (Module module : modules) { if (module.isDisposed()) continue; unlinkModuleFromExternalSystem(module); } ExternalSystemApiUtil.executeOnEdt(true, () -> { AtomicInteger counter = project.getUserData(ORPHAN_MODULE_HANDLERS_COUNTER); if (counter == null) { counter = new AtomicInteger(); project.putUserData(ORPHAN_MODULE_HANDLERS_COUNTER, counter); } counter.incrementAndGet(); Set<Path> orphanModules = project.getUserData(ORPHAN_MODULE_FILES); if (orphanModules == null) { orphanModules = new LinkedHashSet<>(); project.putUserData(ORPHAN_MODULE_FILES, orphanModules); } LocalHistoryAction historyAction = LocalHistory.getInstance().startAction(ExternalSystemBundle.message("local.history.remove.orphan.modules")); try { String rootProjectPathKey = String.valueOf(projectData.getLinkedExternalProjectPath().hashCode()); Path unlinkedModulesDir = ExternalProjectsDataStorage.getProjectConfigurationDir(project).resolve("orphanModules").resolve(rootProjectPathKey); if (!FileUtil.createDirectory(unlinkedModulesDir.toFile())) { LOG.warn("Unable to create " + unlinkedModulesDir); return; } AbstractExternalSystemLocalSettings<?> localSettings = ExternalSystemApiUtil.getLocalSettings(project, projectData.getOwner()); SyncType syncType = localSettings.getProjectSyncType().get(projectData.getLinkedExternalProjectPath()); for (Module module : modules) { if (module.isDisposed()) continue; String path = module.getModuleFilePath(); if (!ApplicationManager.getApplication().isHeadlessEnvironment() && syncType == SyncType.RE_IMPORT) { try { // we need to save module configuration before dispose, to get the up-to-date content of the unlinked module iml StoreUtil.saveSettings(module); VirtualFile moduleFile = module.getModuleFile(); if (moduleFile != null) { Path orphanModulePath = unlinkedModulesDir.resolve(String.valueOf(path.hashCode())); FileUtil.writeToFile(orphanModulePath.toFile(), moduleFile.contentsToByteArray()); Path orphanModuleOriginPath = unlinkedModulesDir.resolve(path.hashCode() + ".path"); FileUtil.writeToFile(orphanModuleOriginPath.toFile(), path); orphanModules.add(orphanModulePath); } } catch (Exception e) { LOG.warn(e); } } modelsProvider.getModifiableModuleModel().disposeModule(module); ModuleBuilder.deleteModuleFile(path); } } finally { historyAction.finish(); } }); }