Shrinking a database file is performed by manipulating pages within the
file such that all empty pages reside at the end of the file. Non-empty
pages from the end of the file are written to empty pages at the start of
the file. Repeating this process, the correct number of times, results in
all of the empty pages residing at the end of the file. Once this is
achieved, the file is truncated such that the space consumed by all empty
and discardable pages is returned to the file system. Truncating the file
involves traversing the file backwards from the end of the file until the
first non-empty page is found. The file is truncated after the last
non-empty page.