Share this page 

Delete a document in a Sharepoint library Tag(s): Networking

To delete a document in a Sharepoint library, we are using the REST API provided by Microsoft. All we need is the Apache HttpComponents library to make the call.

If you are using Maven, here is the POM






import org.apache.http.HttpHost;

import org.apache.http.auth.AuthScope;

import org.apache.http.auth.NTCredentials;

import org.apache.http.client.CredentialsProvider;

import org.apache.http.client.methods.CloseableHttpResponse;

import org.apache.http.client.methods.HttpHead;

import org.apache.http.client.methods.HttpDelete;

import org.apache.http.client.protocol.HttpClientContext;

import org.apache.http.impl.client.BasicCredentialsProvider;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.util.EntityUtils;

public class HttpDeleteWithHttpClient {


   * delete a file in a sharepoint library


   public static void main(String[] args) throws Exception{

     CloseableHttpClient httpclient = HttpClients.custom()

      .setRetryHandler(new DefaultHttpRequestRetryHandler(0,false))


     String user = "myusername";

     String pwd = "mypassword";

     CredentialsProvider credsProvider = new BasicCredentialsProvider();


        new NTCredentials(user, pwd, "", ""));

     // You may get 401 if you go through a load-balancer.

     // To fix this, go directly to one the sharepoint web server or

     // change the config. See this article :


     HttpHost target = new HttpHost("web.mysharepoint.local", 80, "http");

     HttpClientContext context = HttpClientContext.create();


     // The authentication is NTLM.

     // To trigger it, we send a minimal http request

     HttpHead request1 = new HttpHead("/");

     CloseableHttpResponse response1 = null;

     try {

       response1 = httpclient.execute(target, request1, context);


       System.out.println("1 : " + response1.getStatusLine().getStatusCode());


     finally {

       if (response1 != null ) response1.close();


     // The real request, reuse authentication

     HttpDelete request2 = new HttpDelete("/30500C/PubDoc/TEST/jira.log");  // target

     CloseableHttpResponse resp = httpclient.execute(target,request2, context);

     System.out.println("2 : " + resp.getStatusLine().getStatusCode());



     // 204 No Content

     //     The server successfully processed the request, but is not returning any content.

     //     Usually used as a response to a successful delete request.

     // other

     //     we have a problem



See also :