void (@NotNull Project project, Module @Nullable [] modules, @Nullable List<? extends UnloadedModuleDescription> unloadedModules) { final ModuleManager moduleManager = ModuleManager.getInstance(project); final Module[] currentModules = moduleManager.getModules(); final ModifiableModuleModel modifiableModuleModel = moduleManager.getModifiableModel(); final Map<Module, ModifiableRootModel> otherModuleRootModels = new HashMap<>(); Set<String> moduleNamesToDelete = getModuleNamesToDelete(modules, unloadedModules); for (final Module otherModule : currentModules) { if (!moduleNamesToDelete.contains(otherModule.getName())) { otherModuleRootModels.put(otherModule, ModuleRootManager.getInstance(otherModule).getModifiableModel()); } } removeDependenciesOnModules(moduleNamesToDelete, otherModuleRootModels.values()); if (modules != null) { for (final Module module : modules) { for (ProjectAttachProcessor processor : ProjectAttachProcessor.EP_NAME.getExtensionList()) { processor.beforeDetach(module); } modifiableModuleModel.disposeModule(module); } } final ModifiableRootModel[] modifiableRootModels = otherModuleRootModels.values().toArray(new ModifiableRootModel[0]); ModifiableModelCommitter.multiCommit(modifiableRootModels, modifiableModuleModel); if (unloadedModules != null) { moduleManager.removeUnloadedModules(unloadedModules); } }