Retrofit 2 — How to Delete Objects on the Server

In the Retrofit getting started tutorial you've learned how to create an Android app that connects to the public GitHub API and accesses objects on the server. In following tutorials you've learned how to create new objects and update existing ones.

In this tutorial you'll learn how delete objects on the server. This is usually done with DELETE HTTP requests. If you're not familiar with HTTP requests yet, we recommend our tutorial on network basics.

If you're ready to go, let's start!

Retrofit Series Overview

Delete Objects via an API

If you've read the previous tutorials, you know how to access, create and change objects. For us app developers deleting objects is actually the simplest request!

Of course, this only works if the server supports the DELETE operation. Before you start implement the content of this tutorial, make sure your API supports this operation!

Let's review how our Retrofit interface declaration looks in the update Gist objects tutorial:

public class Gist {  
    // all Gist properties  
      // ...
}

public interface GistService {  
    // access Gists 
      @GET("gists")
    Call<List<Gist>> getGists();

      // create new Gist
    @POST("gists")
    Call<ResponseBody> createGist(@Body Gist gist);

      // update existing Gist
    @PATCH("gists/{id}")
    Call<ResponseBody> patchGist(@Path("id") String id, @Body Gist gist);

    @PUT("gists/{id}")
    Call<ResponseBody> replaceGist(@Path("id") String id, @Body Gist gist);

      // delete Gist
      // ... coming up!
}

DELETE HTTP Request

Before you can implement the interface declaration, you've to learn how the request itself looks. Because you don't need to send a payload, and are simply sending a command the request is simple:

DELETE /gists/12343

{}

In summary, all you need is the object ID to specify which object you want. The request doesn't have a payload, and you need to use the DELETE HTTP method.

DELETE Implementation in Retrofit

Let's implement this in Retrofit:

public interface GistService {  
    // ...  

      // delete Gist  
      @DELETE("gists/{id}")
    Call<ResponseBody> deleteGist(@Path("id") String id);
}

And that's it! All you really need is the object ID and you're ready to delete whatever you don't like anymore (within your app)!

A last word of warning: make sure deleting an object on the server doesn't have negative side effects in your Android app. For example, if your view of the object is still open, you need to return to a safe state. If you cache objects, you need to remove the deleted object from the cache.

Summary

In this tutorial you've learned how to delete objects on the server. Deleting objects is quite easy with the DELETE HTTP method. Retrofit conveniently offers this as a single line to us developers. Awesome!

Do you have further questions on this topic or about Retrofit in general? Just let us know on Twitter @futurestud_io or leave a comment below.

Enjoy coding & make it rock!

Explore the Library

Find interesting tutorials and solutions for your problems.