Magento Puts -1 in My URLs Can I Remove Programatically?

Magento Puts -1 in My URLs Can I Remove Programatically?

October 18, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento Puts -1 in My URLs Can I Remove Programatically?

Removing “-1” from Magento URLs Programatically

As an experienced Magento developer, I understand your concern about the “-1” suffix appearing in your URLs after reindexing. This issue is often caused by duplicate entries in the URL rewrite table due to multiple stores or different store views.

Before we dive into a programmatic solution, let me explain the underlying mechanism:

Why does Magento use “-1”? Magento uses this convention as an internal identifier for rewritten URLs to handle cases where multiple URLs point to the same page (e.g., when you have a catalog URL rewrite and a CMS page URL rewrite for the same page). The “-1” suffix helps to differentiate these duplicates.

Potential issues with manual removal

Truncating or manually deleting entries from the core_url_rewrite table can cause significant problems:

  1. Loss of SEO data: If you simply delete records, your previously set meta tags and title tags for specific URLs will be lost.
  2. Inconsistent redirect chain: Deleting URLs without proper handling may break existing redirects, leading to 404 errors or unexpected behavior.
  3. Long rebuilding process: Even with optimization, the reindexing process for a large site can take hours or even days.

A safer alternative: Update and deduplicate

Rather than deleting records, we should aim to update and deduplicate them programatically:

  1. Identify duplicate entries by checking if multiple URLs point to the same entity (product, CMS page, etc.).
  2. Choose a canonical URL for each entry.
  3. Delete or update the non-canonical duplicates.

Here’s a code snippet in Magento 2 that demonstrates this approach using the Magento\UrlRewrite\Model\UrlRewriteProvider:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$urlRewriteFactory = $objectManager->get('Magento\UrlRewrite\Model\UrlRewriteFactory');
$allRewrites = $urlRewriteFactory->create()->getCollection();

// Filter and process duplicates
foreach ($allRewrites as $rewrite) {
    if (/* Your logic to identify duplicates */) {
        // Update the canonical record or delete the duplicate
        // $rewrite->delete();
    }
}

Remember, manipulating core Magento data directly can have unintended consequences. It’s always best practice to use Magento’s built-in APIs and models for tasks like this.

Conclusion

While it may be tempting to simply delete the “-1” entries, a more controlled approach is to update and deduplicate them programatically using Magento’s built-in tools and APIs.

This ensures that your SEO data remains intact, your redirects remain consistent, and you minimize the risk of breaking your site. It also allows for easier recovery if something goes wrong during the process.

If you’re looking for a more guided approach or need to optimize this process further for a large-scale Magento installation, I highly recommend checking out our Magento 2 Performance Optimization Guide on Laravel Company’s blog. It covers advanced techniques and best practices for handling such tasks efficiently.

Happy coding!

Catalog Indexing Database Performance

Our network