Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sqlite File Size not decreasing after deleting some rows from database in iOS

Tags:

sqlite

ios

I am programatically trying to check the size of SQLite database stored on the iphone after deleting some rows. The records get deleted, but the file size remains the same. Is there any way I can get the correct size of file after deleting records from sqlite in iOS? Please see the code below:

NSString* databasePath = [[NSString alloc]
                              initWithString: [self.documentsDirectory stringByAppendingPathComponent: self.databaseFilename]];
unsigned long long fileSize = [[[NSFileManager defaultManager]
                                    attributesOfItemAtPath:databasePath error:nil] fileSize];

Thanks!

I cant use the vaccum approach because it deletes all the records . I am deleting 50% of data only, and not all. So this is not duplicate of this question change sqlite file size after "DELETE FROM table"

like image 580
Nilesh Agrawal Avatar asked Mar 04 '16 03:03

Nilesh Agrawal


1 Answers

The size of a SQLite database file does not necessarily shrink when records are deleted. If auto_vacuum is not enabled, it will never shrink unless you perform a vacuum operation on it.

See https://sqlite.org/lang_vacuum.html and https://sqlite.org/pragma.html#pragma_auto_vacuum .

like image 81
Steve Jorgensen Avatar answered Sep 29 '22 01:09

Steve Jorgensen