Welcome, guest | Sign In | My Account | Store | Cart

Modified from Recipe 576842 to support linux

Walks through the all the Firefox profiles in current user account and cleans all *.sqlite files with "vacuum". It makes firefox faster then often.

tested on Ubuntu

For: python 2.6 (2.5+sqlite3), FF 3.5

Python, 51 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python

import os, sqlite3                                                             

# -------------- constants -----------------------------------------

profilAppMain=os.path.join(os.environ["HOME"], ".mozilla/firefox")
profilUser= os.environ["USER"]                                    
profilApp = os.path.join(profilUser,profilAppMain)                

# -------------- functions -----------------------------------------

def searchProfil(profilApp):
    "all firefox profiles"  
    for profil in os.listdir(profilApp):
        profilFull=os.path.join(profilApp, profil)
        searchSqlite(profilFull)                  
                                                  

def searchSqlite(profil):
    "all sqlite file in each firefox profile"
                                             
    if not os.path.isdir(profil):            
        return

    sq=[os.path.join(profil,s) for s in os.listdir(profil) if s.endswith(".sqlite")]
    print "\n..."+profil[len(profilUser):]
    for s in  sq:
        dirName, fileName=os.path.split(s)
        conn = sqlite3.connect(s)
        old=os.path.getsize(s)
        print fileName+":",
        try:
            c=conn.cursor()
            c.execute("VACUUM")  # this is the thing
            c.close()
            print "done.",
            new=os.path.getsize(s)
            print new*1.0/old*100,"%"
        except:
            print "error."


# ----------------- main -------------------------------------------

if __name__=="__main__":

    if os.path.isdir(profilApp):
        searchProfil(profilApp)
    else:
        print "Not exist:", profilApp

3 comments

peekaa 14 years, 8 months ago  # | flag

shouldnt be here at first line something like?

#!/usr/bin/env python
Vince Spicer (author) 14 years, 8 months ago  # | flag

Good call, updated

skierpage 14 years, 7 months ago  # | flag

(I found this because I wanted to clean my sqlite files without installing the sqlite3 command-line. There's a line of JavaScript you can run in the Firefox Error Console --

Components.classes["@mozilla.org/browser/nav-history-service;1"].getService(Components.interfaces.nsPIPlacesDatabase).DBConnection.executeSimpleSQL("VACUUM");

-- but it only cleans places.sqlite.)

Pasting parts of the script into the python interpreter command line, I found I had to remove the blank line 25 after return within def searchSqlite() or I'd get "IndentationError: unexpected indent".

One flaw in this script is it assumes your profiles are in the profilApp directory. If you've got profiles elsewhere, then a more comprehensive approach would be to parse profile.ini and use the Path value when IsRelative=0 to figure out where profiles are.

Anyway, thanks! I learned an extra smidgen of Python - 97% remaining :-)

Created by Vince Spicer on Wed, 15 Jul 2009 (MIT)
Python recipes (4591)
Vince Spicer's recipes (1)

Required Modules

Other Information and Tasks