I´m sharing with you a new pruneDatabase function. If you ever asked yourself how to get rid of files associated with notes wich are deleted, then this function is for you.

It´s tested against OS 4.5.1b fresh install, and I don´t know if it will work in other versions.

Code:
function pruneDatabase() {
	$GLOBALS['log']->info('----->Scheduler fired job of type pruneDatabase()');
	$backupDir	= 'cache/backups';
	$backupFile	= 'backup-pruneDatabase-GMT0_'.gmdate('Y_m_d-H_i_s', strtotime('now')).'.php';

        $uploadDir = 'cache/upload';

	$db = PearDatabase::getInstance();
	$tables = $db->getTablesArray();

//_ppd($tables);
	if(!empty($tables)) {
		foreach($tables as $kTable => $table) {
			// find tables with deleted=1
			$qDel = 'SELECT * FROM '.$table.' WHERE deleted = 1';
			$rDel = $db->query($qDel);// OR continue; // continue if no 'deleted' column

            if ($table == 'notes') {
                // create an array with notes wich have attachments
                $DocDel = 'SELECT id FROM '.$table.' WHERE deleted = 1 AND filename IS NOT NULL';
                $dDel = $db->query($DocDel);

                $DocsArray = array();

                while($lDel = $db->fetchByAssoc($dDel)) {
                    $DocsArray[] = $lDel['id'];
                }

                foreach ($DocsArray as $Doc) {

                    unlink($uploadDir.'/'.$Doc);
                }
            }

			// make a backup INSERT query if we are deleting.
			while($aDel = $db->fetchByAssoc($rDel)) {
				// build column names
				$rCols = $db->query('SHOW COLUMNS FROM '.$table);
				$colName = array();

				while($aCols = $db->fetchByAssoc($rCols)) {
					$colName[] = $aCols['Field'];
				}

				$query = 'INSERT INTO '.$table.' (';
				$values = '';
				foreach($colName as $kC => $column) {
					$query .= $column.', ';
					$values .= '"'.$aDel[$column].'", ';
				}

				$query  = substr($query, 0, (strlen($query) - 2));
				$values = substr($values, 0, (strlen($values) - 2));
				$query .= ') VALUES ('.str_replace("'", "'", $values).');';

				$queryString[] = $query;

				if(empty($colName)) {
					$GLOBALS['log']->fatal('pruneDatabase() could not get the columns for table ('.$table.')');
				}
			} // end aDel while()
			// now do the actual delete
			$db->query('DELETE FROM '.$table.' WHERE deleted = 1');
		} // foreach() tables

		// now output file with SQL
		if(!function_exists('mkdir_recursive')) {
			require_once('include/dir_inc.php');
		}
		if(!function_exists('write_array_to_file')) {
			require_once('include/utils/file_utils.php');
		}
		if(!file_exists($backupDir) || !file_exists($backupDir.'/'.$backupFile)) {
			// create directory if not existent
			mkdir_recursive($backupDir, false);
		}
		// write cache file

		write_array_to_file('pruneDatabase', $queryString, $backupDir.'/'.$backupFile);
		return true;
	}
	return false;
}