After Ansem is saved from a group of Nobodies at The World That Never Was by King Mickey, the two travel together for a short time, and Ansem explains the purpose of the machine that he has with him; he means to use it to convert the Kingdom Hearts created by Organization XIII (with the unwitting help of Sora, Donald, and Goofy) into data so that it will not harm anyone else.
However, Kingdom Hearts proves to be too much for the Encoder to handle, and the machine begins to overload while Ansem futilely attempts to contain it. In the end, Sora, Donald, Goofy, Mickey, Riku, and Kairi attempt to stop him from doing what he plans to do, but Ansem begs them not to, he gives Riku and Mickey some parting words.
He tells Sora that the rest is up to him, and sincerely apologizes through Sora to Roxas for what he had done to him. Xemnas appears afterward, and after conversing with his former apprentice, the machine self-destructs, sending Ansem to the Realm of Darkness and destroying most of his memories.
However, the machine also undoes the majority of the Organization's work on Kingdom Hearts and completely purifies Riku of the presence of Ansem, Seeker of Darkness, returning him to his normal form. But by doing this, it caused a massive birth of Heartless as the hearts touched the ground down in the city below, and other Nobodies arrived to try and catch the falling hearts.