In those situations, you don't simply trash away the old code and restart from the scratch; you do a bunch of small and gradual changes, while keeping the software functional through the whole process. Identify the most pressing issues, refactor that specific bad piece of code, repeat as needed.
Otherwise, you end with two versions of the same software: old, buggy but full of content vs. new, clean but missing a lot of stuff. And either you stop developing the old version altogether (bad) or you reduplicate efforts backporting new content to that version (also bad).