The procedure is: (not tested...)
Save all your books to disk, making sure to save covers and complete updated metadata in opf:s.
Create a new empty calibre library, with the same structure as the old.
Import all the saved books to the new library, reading metadata from OPF:s.
Enjoy lovely low index numbers without gaps. For a while...
Test first on a small representative subset of books...
It is essentially a database dump and restore from dump. I remember when that was the usual way to speed up and compact databases...