Magento: An error occurred while saving the URL rewrite

In one of our recently projects, I got the following issue when I tried to reindex Catalog URL Rewrites index: "An error occurred while saving the URL rewrite"


After spending time to debug the issue, I came up with file app/code/core/Mage/Catalog/Model/Resource/Url.php and I could see the error in saveRewrite() function.

I echoed the real error message, I found out that issue is because the system could not insert a duplicated record. So something was wrong with "core_url_rewrite" table.

I fixed the issue by logging into phpMyadmin, find that table and truncate all records (do not worry about this because all URL rewrites will be generated again when you reindexed).

After that, I logged in the backend, refresh Magento cache and reindex again. Everything is ok now.
Please comment if you find out this solution is not good or safe enough. Thanks!