Automatically clean database?

I have a program running that automatically upgrades the quality of episodes, each time it does, this happens:
http://i.imgur.com/jSExrft.png

I have to clean the database constantly because of it. Is there anyway to automatically clean the database, or automatically remove items from the database that no longer exist?

EDIT: Answered my own question. Made an advancedsettings.xml with <cleanonupdate>true</cleanonupdate> in it.