If you are worried about the cleanup operation having python code running in it, maybe you could use the SQLite CLI to run that operation instead.