
clean_multimodel <- function(recipe, files_hcst, files_fcst, files_obs) {
  
  # Names of the models
  if (tolower(recipe$Analysis$Horizon) == 'seasonal') {
    exp.name <- recipe$Analysis$Datasets$System$models
  } else if (tolower(recipe$Analysis$Horizon) == 'decadal') {
    exp.name <- sapply(recipe$Analysis$Datasets$System$models, '[[', 'name')
  } else {stop('Multimodel not implemented for this horizon.')}
  exp.name <- gsub('\\.','',exp.name)
  
  # To remove the observations from all models' folders
  for (m in exp.name[-1]){
    files_obs <- c(files_obs, gsub(exp.name[1],m,files_obs))
  }
  
  if (tolower(recipe$Analysis$Datasets$Multimodel$execute) %in% c('true','yes')) {
    
    # Keep only multimodel data
    
    # Remove createFrom files
    unlink(files_hcst)
    unlink(files_fcst)
    unlink(files_obs)
    
    # Remove Skill and plots file of systems
    unlink(file.path(recipe$Run$output_dir,"outputs/*",exp.name),
           recursive = T)
    unlink(file.path(recipe$Run$output_dir,"plots/*",exp.name),
           recursive = T)
    
  } else if (tolower(recipe$Analysis$Datasets$Multimodel$execute) == 'both' &&
             recipe$Analysis$Workflow[[recipe$Analysis$Datasets$Multimodel$createFrom]]$save == 'none') {
    
    # Remove createFrom files only
    unlink(files_hcst)
    unlink(files_fcst)
    unlink(files_obs)
    
  }
  
}
