title: Files remain open/locked in Azure Function - unable to delete
body: <p>My program makes use of a <a href="https://github.com/kikovalle/PLGSharepointRestAPI-java" rel="nofollow noreferrer">library</a> to upload a file located in an Azure File Share to Sharepoint, after which the file is deleted from Azure File Share. Below is a small (the relevant) part of my code; when I run it the file is uploaded correctly, but isn't removed afterwards isn't removed because it is still in use by an SMB client (it's &quot;marked for deletion&quot;, but is only deleted once the Azure Function is disabled).</p>
  <p>My guess was that since an InputStream is opened in the wrapper.uploadFile, but not closed that might be it, but <code>resource.isOpen()</code> always returns <code>false</code></p>
  <p><strong>main.class</strong></p>
  <pre><code>    File file = new File (filepath);
      Resource resource = new FileSystemResource(filepath);
      PLGSharepointClient wrapper = new PLGSharepointClient(user, passwd, domain, spSiteUrl);   
      JSONObject jsonMetadata = new JSONObject();
      wrapper.uploadFile(spFolder, resource, jsonMetadata);
      
      resource.getInputStream().close();
      System.out.println(resource.isOpen());
      file.delete();
  </code></pre>
  <p><strong>wrapper.uploadFile</strong></p>
  <pre><code>
      public JSONObject uploadFile(String folder, Resource resource, JSONObject jsonMetadata) throws Exception {
              LOG.debug(&quot;Uploading file {} to folder {}&quot;, resource.getFilename(), folder);
              JSONObject submeta = new JSONObject();
              submeta.put(&quot;type&quot;, &quot;SP.ListItem&quot;);
              jsonMetadata.put(&quot;__metadata&quot;, submeta);
              
              headers = headerHelper.getPostHeaders(&quot;&quot;);
              headers.remove(&quot;Content-Length&quot;);
              
              byte[] resBytes = IOUtils.readFully(resource.getInputStream(), (int) resource.contentLength());
       
              RequestEntity&lt;byte[]&gt; requestEntity = new RequestEntity&lt;&gt;(resBytes, 
                  headers, HttpMethod.POST, 
                  this.tokenHelper.getSharepointSiteUrl(
                          &quot;/_api/web/GetFolderByServerRelativeUrl('&quot; + UriUtils.encodeQuery(folder, StandardCharsets.UTF_8) +&quot;')/Files/add(url='&quot; 
                          + UriUtils.encodeQuery(resource.getFilename(), StandardCharsets.UTF_8) + &quot;',overwrite=true)&quot;
                          )
                  );
      
              ResponseEntity&lt;String&gt; responseEntity = 
                  restTemplate.exchange(requestEntity, String.class);
      
              String fileInfoStr = responseEntity.getBody();
              
              LOG.debug(&quot;Retrieved response from server with json&quot;);
              
              JSONObject jsonFileInfo = new JSONObject(fileInfoStr);
              String serverRelFileUrl = jsonFileInfo.getJSONObject(&quot;d&quot;).getString(&quot;ServerRelativeUrl&quot;);
      
              LOG.debug(&quot;File uploaded to URI&quot;, serverRelFileUrl);
              String metadata = jsonMetadata.toString();
              headers = headerHelper.getUpdateHeaders(metadata);
      
              LOG.debug(&quot;Updating file adding metadata {}&quot;, jsonMetadata);
      
              RequestEntity&lt;String&gt; requestEntity1 = new RequestEntity&lt;&gt;(metadata, 
                  headers, HttpMethod.POST, 
                  this.tokenHelper.getSharepointSiteUrl(&quot;/_api/web/GetFileByServerRelativeUrl('&quot; + UriUtils.encodeQuery(serverRelFileUrl, StandardCharsets.UTF_8) + &quot;')/listitemallfields&quot;)
                  );
              ResponseEntity&lt;String&gt; responseEntity1 = 
                      restTemplate.exchange(requestEntity1, String.class);
              LOG.debug(&quot;Updated file metadata Status {}&quot;, responseEntity1.getStatusCode());
              return jsonFileInfo;
          }
  </code></pre>