title: Spring Integration FTP remove local files after use (Spring Boot)
body: <p>I am trying to write a program that can take a file from one server via ftp and place it on another server via ftp. However, I am having issues deleting the local file after it has been written. Being able to save it locally is not an issue as long as it is temporary. I have tried using an ExpressionEvaluatingRequestHandlerAdvice with an OnSuccessExpression and I could not get it to actually use the expression. The code is here:</p>
  
  <pre><code>@Configuration
  @EnableConfigurationProperties(FTPConnectionProperties.class)
  public class FTPConfiguration {
  
      private FTPConnectionProperties ftpConnectionProperties;
  
      public FTPConfiguration(FTPConnectionProperties ftpConnectionProperties) {
          this.ftpConnectionProperties = ftpConnectionProperties;
      }
  
      @Bean
      public SessionFactory&lt;FTPFile&gt; ftpInputSessionFactory() {
          DefaultFtpSessionFactory sf = new DefaultFtpSessionFactory();
          sf.setHost(ftpConnectionProperties.getInputServer());
          sf.setUsername(ftpConnectionProperties.getInputFtpUser());
          sf.setPassword(ftpConnectionProperties.getInputFtpPassword());
          return new CachingSessionFactory&lt;&gt;(sf);
      }
  
      @Bean
      public SessionFactory&lt;FTPFile&gt; ftpOutputSessionFactory() {
          DefaultFtpSessionFactory sf = new DefaultFtpSessionFactory();
          sf.setHost(ftpConnectionProperties.getOutputServer());
          sf.setUsername(ftpConnectionProperties.getOutputFtpUser());
          sf.setPassword(ftpConnectionProperties.getOutputFtpPassword());
          return new CachingSessionFactory&lt;&gt;(sf);
      }
  
      @Bean
      public FtpInboundFileSynchronizer ftpInboundFileSynchronizer() {
          FtpInboundFileSynchronizer fileSynchronizer = new FtpInboundFileSynchronizer(ftpInputSessionFactory());
          fileSynchronizer.setDeleteRemoteFiles(true);
          fileSynchronizer.setRemoteDirectory(ftpConnectionProperties.getInputDirectory());
          fileSynchronizer.setFilter(new FtpSimplePatternFileListFilter("*.TIF"));
          return fileSynchronizer;
      }
  
      @Bean
      @InboundChannelAdapter(channel = "input", poller = @Poller(fixedDelay = "5000"))
      public MessageSource&lt;File&gt; ftpMessageSource() {
          FtpInboundFileSynchronizingMessageSource source = new FtpInboundFileSynchronizingMessageSource(ftpInboundFileSynchronizer());
          source.setLocalDirectory(new File("ftp-inbound"));
          source.setAutoCreateLocalDirectory(true);
          source.setLocalFilter(new FileSystemPersistentAcceptOnceFileListFilter(new SimpleMetadataStore(), ""));
          return source;
      }
  
      @Bean
      @ServiceActivator(inputChannel = "input")
      public MessageHandler handler() {
          FtpMessageHandler handler = new FtpMessageHandler(ftpOutputSessionFactory());
          handler.setRemoteDirectoryExpression(new LiteralExpression(ftpConnectionProperties.getOutputDirectory()));
          handler.setFileNameGenerator(message -&gt; {
              if (message.getPayload() instanceof File) {
                  return ((File) message.getPayload()).getName();
              } else {
                  throw new IllegalArgumentException("File expected as payload.");
              }
          });
          return handler;
      }
  
  }
  </code></pre>
  
  <p>It is handling the remote files exactly as expected, deleting the remote file from the source and putting into the output, but not removing the local file after use.</p>