http://www.sqlite.org/lang_vacuum.html
The VACUUM command rebuilds the entire database. There are several reasons an application might do this:
Unless SQLite is running in "auto_vacuum=FULL" mode, when a largeamount of data is deleted from the database file it leaves behind emptyspace, or "free" database pages. This means the database file mightbe larger than strictly necessary. Running VACUUM to rebuild the database reclaims this space and reduces the size of the database file.
Frequent inserts, updates, and deletes can cause the database fileto become fragmented - where data for a single table or index is scattered around the database file. Running VACUUM ensures that each table andindex is largely stored contiguously within the database file. In somecases, VACUUM may also reduce the number of partially filled pages inthe database, reducing the size of the database file further.
Normally, the database page_sizeand whether or not the databasesupports auto_vacuummust be configured before the database file isactually created. However, when not in write-ahead logmode, the page_sizeand/or auto_vacuumproperties of an existing database may bechanged by using the page_sizeand/or pragma auto_vacuumpragmas and then immediately VACUUMingthe database. When in write-ahead logmode, only the auto_vacuumsupport property can be changed using VACUUM.
VACUUM only works on the main database. It is not possible to VACUUM anattached database file.
The VACUUM command works by copying the contents of the database intoa temporary database file and then overwriting the original with the contents of the temporary file. When overwriting the original, a rollbackjournal or write-ahead logWAL file is used just as it would be for anyother database transaction. This means that when VACUUMing a database, as much as twice the size of the original database file is required in freedisk space.
The VACUUM command may change the ROWIDsof entries in anytables that do not have an explicit INTEGER PRIMARY KEY.
A VACUUM will fail if there is an open transaction, or if there are one ormore active SQL statements when it is run.
As of SQLite version 3.1, an alternative to using the VACUUM command toreclaim space after data has been deleted is auto-vacuum mode, enabled usingthe auto_vacuumpragma. When auto_vacuumis enabled for a databasefree pages may be reclaimed after deleting data, causing the file to shrink,without rebuilding the entire database using VACUUM. However, usingauto_vacuumcan lead to extra database file fragmentation. And auto_vacuumdoes not compact partially filled pages of the database as VACUUM does.