Outlook 2016 Database Rebuild -AppleScript-

clrlmiller
New Contributor III

Our facility must sometimes 'clean' a system when data that's not supposed to be in the open, ends up in the open and on a system. The usual avenue for this data finding it's way onto a system is email and we must purge the targeted email in conducting the 'cleaning' of the system. This also means rebuilding the email database to ensure the deleted emails CAN'T be recovered.

Under Outlook 2011 Mac, this was performed simply by holding the option key during launch and rebuilding the mail database. Outlook 2016 has removed this feature and the official support from Microsoft is that Outlook will determine when a database needs rebuilt and users shouldn't do this anyway. Well, we still need to do this when cleaning a system.

Luckily someone made a post on how to do this on a SQLite database which Outlook 2016 uses. Post Here: http://www.cortig.net/wordpress/2016/04/25/outlook-2016-sqlite-database-commands/ I tried making this a little easier for our field technicians and wrapped it into an AppleScript App. Feel free to use, it seems to be working for us.

FOLLOWUP EDIT: I've added a link on a comment below on GitHub for a cleaner display of text.

##################################################################
# Prepare for cleaning/rebuilding User's Outlook 2016 database
# By: Christopher Miller Dated: 2016-12-01, LastMod: 2016-12-06
# For: ITSD-ISS of JHU-APL
# Taken From: http://www.cortig.net/wordpress/2016/04/25/outlook-2016-sqlite-database-commands/
##################################################################

####################################
# Issue warning before closing Outlook Application
####################################
tell application "Finder"
    activate
    display dialog "This app will be modifying the Outlook 2016 database files and could damage data.  Please ensure a backup is ready BEFORE proceeding.  Hit -OK- to continue or -Cancel- to quit" buttons {"Cancel", "OK"} default button {"Cancel"} with title "WARNING" with icon stop
end tell

####################################
# Check if open and if so, Close the Outlook Application
####################################
tell application "System Events"
    repeat until ("Microsoft Outlook" is not in name of processes) is true
        tell application "Microsoft Outlook" to quit
    end repeat
end tell


#########################################
# Query the Technician to choose a user account to manipulate
#########################################
set FileThere to "false"
repeat until FileThere is "true"
    tell application "System Events"
        activate
        do shell script "/bin/ls /Users | egrep -i -v Shared"
        get paragraphs of result
        choose from list (result) with prompt "Select the User Account you would like to clean" with title "CHOOSE USER"
        if the result is not false then
            set UserAcct to item 1 of the result
        else if the result is false then
            return
        end if

        ###################################
        # Check if the database files exists for the User Accont
        ###################################
        tell application "Finder"
            if exists "/Users/" & UserAcct & "/Library/Group Containers/UBF8T346G9.Office/Outlook/Outlook 15 Profiles/Main Profile/Data/Outlook.sqlite" as POSIX file then
                set FileThere to "true"
            else
                tell application "Finder"
                    activate
                    display dialog "Sorry, but an Outlook SQLite database file wasn't found for the account " & UserAcct & ", would you like to try again?" buttons {"Cancel", "Try Again"} default button {"Try Again"} with title "ERROR" with icon 2
                end tell
            end if
        end tell
    end tell
end repeat



##########################################
# Find the Size of the Database Files BEFORE doing a vacuuming
##########################################
tell application "System Events"
    set PreSizeMain to do shell script "/bin/ls -ho /Users/" & UserAcct & ""/Library/Group Containers/UBF8T346G9.Office/Outlook/Outlook 15 Profiles/Main Profile/Data/Outlook.sqlite" | awk {'print $4'}"

    set PreSizeWal to do shell script "/bin/ls -ho /Users/" & UserAcct & ""/Library/Group Containers/UBF8T346G9.Office/Outlook/Outlook 15 Profiles/Main Profile/Data/Outlook.sqlite-wal" | awk {'print $4'}"
end tell



##################################
# Vacuum the database files for the chosen account 
##################################
try
    do shell script "/usr/bin/sqlite3 /Users/" & UserAcct & ""/Library/Group Containers/UBF8T346G9.Office/Outlook/Outlook 15 Profiles/Main Profile/Data/Outlook.sqlite" vacuum;"

    #do shell script "/usr/bin/sqlite3 /Users/"& UserAcct &""/Library/Group Containers/UBF8T346G9.Office/Outlook/Outlook 15 Profiles/Main Profile/Data/Outlook.sqlite-shm" vacuum;"

    do shell script "/usr/bin/sqlite3 /Users/" & UserAcct & ""/Library/Group Containers/UBF8T346G9.Office/Outlook/Outlook 15 Profiles/Main Profile/Data/Outlook.sqlite-wal" vacuum;"
end try



##########################################
# Find the Size of the Database Files AFTER doing a vacuuming
##########################################
tell application "System Events"
    set PostSizeMain to do shell script "/bin/ls -ho /Users/" & UserAcct & ""/Library/Group Containers/UBF8T346G9.Office/Outlook/Outlook 15 Profiles/Main Profile/Data/Outlook.sqlite" | awk {'print $4'}"

    set PostSizeWal to do shell script "/bin/ls -ho /Users/" & UserAcct & ""/Library/Group Containers/UBF8T346G9.Office/Outlook/Outlook 15 Profiles/Main Profile/Data/Outlook.sqlite-wal" | awk {'print $4'}"
end tell


#####################################
# Tell Technician we're done and relay before/after sizes
#####################################
tell application "Finder"
    activate
    display dialog "Vacuuming of Outlook 2016 database completed,
    Please Launch and verify Mail is okay!

Main Size was: " & PreSizeMain & " and is now: " & PostSizeMain & "
Wal Size was: " & PreSizeWal & " and is now: " & PostSizeWal & "" with title "DONE" with icon 1 giving up after 60
end tell



return

0a7a56588f2444e7a6c1e0312b89631f

2 ACCEPTED SOLUTIONS

rquigley
Contributor

Might be better to format it with the ``` at the beginning and end of the script to ensure its easier to copy.

View solution in original post

jhbush
Valued Contributor II

@clrlmiller could you repost this here using the code flags or on GitHub?

View solution in original post

10 REPLIES 10

rquigley
Contributor

Might be better to format it with the ``` at the beginning and end of the script to ensure its easier to copy.

jhbush
Valued Contributor II

@clrlmiller could you repost this here using the code flags or on GitHub?

clrlmiller
New Contributor III

@jhbush1973 I'm new to GitHub, but I think this is what you're looking for.
https://github.com/ctmiller/Outlook2016_Database_Rebuild

chad_fox
Contributor II

Thanks for sharing, this will definitely come in handy.

rqomsiya
Contributor III

This looks like it no longer is working. Keeps throwing up an error saying it can't find the .sqlite.

Any ideas?

clrlmiller
New Contributor III

Hi @rqomsiya Can you provide any more details on your Mac system setup? {OS Version, Outlook version, etc.}
I'm running the latest Outlook 2016 version 15.32 and this still seems to be working. Also, are you sure you're looking to clean up the correct user account?

-CTM

rqomsiya
Contributor III

Hi @clrlmiller . I'm running 15.31 on 10.10.5. I'm currently logged in as the user (myself). I select the user.

I noticed the script references this location, which I don't seem to have:

 if exists "/Users/" & UserAcct & "/Library/Group Containers/UBF8T346G9.Office/Outlook/Outlook 15 Profiles/Main Profile/Data/Outlook.sqlite" as POSIX file then

Thanks,
Ronnie

rqomsiya
Contributor III

Actually, looks like it is there under ~/Library/

rqomsiya
Contributor III

Here is my path to my ~/Library/33424084618542cf90bde999cf44e1bb

clrlmiller
New Contributor III

HI @rqomsiya , From the looks of it, the profile you're using in Outlook 2016 is NOT the 'Main' profile, and has instead been named/setup as 'RYQ'. You should be able to rename this profile using the instructions here: https://support.office.com/en-us/article/Manage-profiles-in-Outlook-2016-for-Mac-fed2a955-74df-4a24-bef6-78a426958c4c

I could build a sub-portion into the script for doing a directory listing (LS) of the 'Outlook 15 Profiles' Directories for further choice or just taking action on every single profile found. If this seems to be an issue, I can re-visit the script. Anyway, rename the profile to 'Main' and you should be good to go.

-CTM