"When She Loved Me" is a song written by American musician Randy Newman and recorded by Canadian singer Sarah McLachlan for Pixar's animated film Toy Story 2 (1999). The song is sung from the perspective of character Jessie, a toy cowgirl, as she reveals her backstory by reflecting upon her defunct relationship with her original owner, Emily, who outgrew her. Heard in the film during a flashback sequence, the filmmakers decided to incorporate a song into the montage during which Jessie details her backstory to Woody after multiple attempts to show the character relaying her experience verbally proved unsuccessful.

Newman initially felt that the song was inappropriate, doubting that young children would be interested in it; he changed his mind after screen tests showed promising results. The song was offered to McLachlan after Newman and the filmmakers agreed that the ballad was more appropriate for a female artist. Despite some hesitation from her management, McLachlan greatly enjoyed the ballad and agreed to record it upon hearing Newman's demo, finding herself drawn towards its melancholy nature. Musically, "When She Loved Me" is an emotional pop ballad backed by simple piano accompaniment. Various interpretations of the song's lyrics and themes have been offered; while written primarily about the pain felt upon losing a platonic friend, "When She Loved Me" has also been interpreted as a love song, while some music journalists consider the track to be a metaphor for children inevitably growing up and becoming independent from their parents.

"When She Loved Me" has garnered critical acclaim from film and music critics, who found the song to be both moving and heartbreaking, praising Newman's songwriting and McLachlan's vocal performance. "When She Loved Me" won a Grammy Award for Best Song Written for a Motion Picture, Television or Other Visual Media. The song was nominated for Golden Globe and Academy Awards for Best Original Song. "When She Loved Me" has since earned a reputation as one of the saddest sequences in both Pixar and Disney films, as well as one of the saddest film songs ever written. The song has been covered by several artists, including Steve Tyrell, Bridgit Mendler and the musical group Steps.

Writing and recording

Beginning with Toy Story (1995), director John Lasseter had long decided Pixar films would not be musicals, but agreed that musician Randy Newman would write original songs for certain story moments, most of which Newman would sing himself. Toy Story 2'''s filmmakers originally struggled to have Jessie, a toy cowgirl, simply convey her backstory with dialogue, eventually reworking her thoughts into a song called "When She Loved Me". Pixar tasked Newman with writing a song expressing Jessie's disappointment in her former owner as the latter grows up, with the songwriter only knowing the song's required length and gender. Lasseter described the addition of Jessie's song as the most significant change the film underwent during production. Newman initially doubted a song about being loved versus unloved would work in a children's film, and Disney had expressed concerns about losing audience interest during slow songs in the past. At a Toy Story 2 test screening, Newman was surprised to find that even the youngest children appeared to remain attentive during the song, despite its slow tempo and mature, emotional themes. Newman confirmed that the song was not the first time Pixar had proved him wrong, and admitted to being proudest of the work he did on "When She Loved Me" for the film.

Alongside "Woody's Roundup", "When She Loved Me" was one of two songs Newman wrote for Toy Story 2. Originally called "Jessie's Song", Newman's demo was shorter than the final version, with slightly different lyrics. He considers "When She Loved Me" to be among his favorite film compositions and one of the best songs he has written for a movie, and found it easier than writing for his own studio albums because of Disney's clear vision and instructions. Lasseter also allowed him a lot of freedom with the song. Although Newman maintains that the song is almost exclusively about the relationship between a doll and her estranged owner, he admitted that some of his own experiences were incorporated. Unlike the songs he had written for Toy Story, Newman does not sing "When She Loved Me" himself because Pixar wanted a female singer, and enlisted Canadian singer Sarah McLachlan to provide Jessie's off-screen singing voice in lieu of her voice actress Joan Cusack. Newman likened writing for McLachlan's voice as opposed to himself to "writing for a different instrument", describing hers as "a voice that can hold notes" compared to his own.

McLachlan's manager warned her that she might not like "When She Loved Me" when they received it, and she claims her manager described it as "kind of weird". McLachlan ultimately immediately cried upon hearing it for the first time, saying Newman's demo reminded her of Jim Henson's original performance of "Bein' Green" as Kermit the Frog. McLachlan felt confident that mothers would be incredibly moved by the song regardless of how children reacted. McLachlan found the recording process different than what she had grown accustomed to owing to the number of people involved in an animated film. Newman and McLachlan recorded approximately 17 takes of the song before finally deciding upon a few with which they were satisfied. They produced the song together, with Newman arranging and orchestrating. They completed it within a few days. Both Newman's piano accompaniment and McLachlan's vocals were recorded by producer Greg Reely at The Warehouse Studio in Vancouver, British Columbia. The singer described the song as a favorite of hers and considers the project a highlight of her career.

Actors Tom Hanks and Tim Allen, the voices of Woody and Buzz Lightyear, respectively, were moved to tears upon watching "When She Loved Me" for the first time.

Context and use in Toy Story 2 Serving as a "haunting soundtrack to Jessie's tale of abandonment", "When She Loved Me" is Toy Story 2's main song, and plays over a montage showing of Jessie and her former owner Emily. Heard approximately midway through the film, the song is sung in-character and off-screen by Jessie during a flashback in which she recalls the moment she is abandoned by Emily, reflecting upon their once-loving relationship and the good times they shared until Jessie is gradually outgrown and ultimately donated to charity in a cardboard box. The montage was animated by Tasha Wedeen. According to co-director Ash Brannon, the scene is "an example of perfect animation casting". In addition to the sequel having more female animators than Toy Story, Brannon found it helpful that a woman animated Jessie, believing no one could have done it better than Wedeen. Specific lighting effects and filters were used throughout the sequence to complement its mood, including sun-kissed lighting.