def commit(self):
        if self.needsCleanup:
            # this join could be slow; it would be much better if we could
            # restrict the select on Instances by instanceId, but that's
            # not so easy and may require multiple passes (since we may
            # now be able to remove a trove which was included by a trove
            # which was included by a trove which was removed; getting that
            # closure may have to be iterative?). that process may be faster
            # then the full join?
            # NOTE: if we could assume that we have weak references this
            # would be a two-step process
            cu = self.db.cursor()

            cu.execute("""DELETE FROM TroveInfo WHERE
                    instanceId IN (
                        SELECT Instances.instanceId FROM Instances
                        WHERE isPresent = 0)
                      """)

            cu.execute("""DELETE FROM Instances WHERE
                    instanceId IN (
                        SELECT Instances.instanceId
                        FROM
                        Instances LEFT OUTER JOIN TroveTroves
                        ON Instances.instanceId = TroveTroves.includedId
                        WHERE isPresent = 0 AND TroveTroves.includedId IS NULL)
                      """)

            cu.execute("""DELETE FROM Versions WHERE Versions.versionId IN
                            (SELECT rmvdVer FROM RemovedVersions
                                LEFT OUTER JOIN Instances ON
                                    rmvdVer == Instances.versionId
                                LEFT OUTER JOIN DBTroveFiles ON
                                    rmvdVer == DBTroveFiles.versionId
                                WHERE
                                    Instances.versionId is NULL AND
                                    DBTroveFiles.versionId is NULL)""")
            cu.execute("DROP TABLE RemovedVersions")
            self.needsCleanup = False

        self.db.commit()
        self.addVersionCache = {}
        self.flavorsNeeded = {}