Deleting Movie nodes

In previous lessons, you created a new Movie node and its relationships in the database from your Java application. Now you will learn how to delete a Movie node and its relationships from the database.

DeleteById() method

Just as with previous derived methods for findAll() or save(), there is a derived method to delete data called deleteById(). You will use this method to delete a Movie node in Neo4j.

  1. Open the MovieController.java file in the src/main/java/com/example/appspringdata directory.

  2. Import the DeleteMapping and RequestParam classes from org.springframework.web.bind.annotation:

    java
    import org.springframework.web.bind.annotation.DeleteMapping;
    import org.springframework.web.bind.annotation.RequestParam;
  3. Add the delete method to the MovieController:

    java
        @DeleteMapping("/delete")
        void delete(@RequestParam String movieId) {
            movieRepo.deleteById(movieId);
            System.out.println("Deleted movie with movieId: " + movieId);
        }

Full code for the MovieController class is available in the dropdown below.

Click to reveal the completed MovieController class code
java
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Optional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestParam;

@RestController
@RequestMapping("/movies")
public class MovieController {
    private final MovieRepository movieRepo;

    public MovieController(MovieRepository movieRepo) {
        this.movieRepo = movieRepo;
    }

    @GetMapping()
    Iterable<Movie> findAllMovies() {
        return movieRepo.findMoviesSubset();
    }

    @GetMapping("/{movieId}")
    Optional<Movie> findMovieById(@PathVariable String movieId) {
        return movieRepo.findById(movieId);
    }

    @PostMapping("/save")
    Movie save(@RequestBody Movie movie) {
        return movieRepo.save(movie);
    }

    @DeleteMapping("/delete")
    void delete(@RequestParam String movieId) {
        movieRepo.deleteById(movieId);
        System.out.println("Deleted movie with movieId: " + movieId);
    }
}

This method is annotated with @DeleteMapping and will handle requests to the /movies/delete endpoint. It passes in a movieId specified as a request parameter at the end of the URL. The deleteById() method is called on the repository with the movieId passed in as an argument. The method then prints a message to the console to confirm the deletion.

Deleting relationships

By default, the delete operation will do a DETACH DELETE, which means it will first delete any relationships connected to the node before deleting the node itself. However, the node at the other end of the relationship will not get deleted.

Testing the deleteById() method

For this example, you will delete the movie created in the last lesson.

Run your application. Once the application is running, go to the terminal tab and execute curl -X DELETE 'localhost:8080/movies/delete?movieId=9876' to delete the MyMovie node created in the last lesson.

Delete Results output (example)
Deleted movie with movieId: 9876

Note: The output statement above will be printed in the terminal where the application is running, rather than where you make the call because the method returns void. Execute the populated query in the right-hand tab to verify that the node has been deleted. You should see no results returned.

Delete movie results

Summary

In this lesson, you learned how to delete a Movie node in Neo4j using the derived deleteById() method.

In the next module, you will define methods with custom Cypher statements for reading, writing, and updating data.