0

I have a list of 3089 301 redirects URLs and want to delete them programmatically from the database.

E.g List of source URLs

/about/test/2013/page_61732.html /about/test/additional/branches/page_38678.html
/about/test/additional/branches/page_38678.html
/about/test/additional/branches/page_38678.html ......

2 Answers 2

2

If I understand your question correctly, you want to delete all redirects with a status code of 301. That could be done like this:

$ids = \Drupal::entityQuery('redirect')
   ->condition('status_code', '301')
   ->execute();

$entities = Redirect::loadMultiple($ids);

\Drupal::entityTypeManager()->getStorage('redirect')->delete($entities);

If you only want to delete redirects that have a certain source URL, you should be able to add

->condition('redirect_source__path', $sources, 'IN')

to your query.

1
  • 1
    Not sure if 3089 entities shouldn't better be processed in a batch process? 🤔
    – leymannx
    Commented Mar 22, 2021 at 12:44
-1
<?php
        use Drupal\redirect\Entity\Redirect;
        
        //list of all source urls
        $newInputs = [
            '/about/*****/2013/page_61732.html',
            '/about/*****/additional/branches/page_38678.html',
            '/about/*****/additional/branches/winners/page_38681.html',
            '/about/*****/additional/development/winners/page_38703.html',
             .....................
        ];

    
        foreach ($newInputs as $input) {
                 $sourceUrl = trim($input);
    
                 $query = \Drupal::entityTypeManager()->getStorage('redirect')->getQuery(); 
                 $redirects = $query->condition('redirect_source.path', substr($sourceUrl, 1))
                           ->execute();
        
                 $storageHandler = \Drupal::entityTypeManager()->getStorage('redirect');
                 $entities = $storageHandler->loadMultiple($redirects);
        
                 foreach ($entities as $entity) {
            
                         //get redirect source path
                          $sourceRedirectUrl = $entity->getSourceUrl();
            
                          if ($sourceRedirectUrl == $sourceUrl)
                             {
                               $entity->delete();
                       
                              }
                           elseif (urldecode($sourceRedirectUrl) == urldecode($sourceUrl))
                              {
                                $entity->delete();
                               }
                           else
                               {
                                print 'Error !! Bad Url in the record';
    
                                Print "Source Url from list : ".urldecode($sourceUrl)." \n Source Redirect Url " .urldecode($sourceRedirectUrl). "\n"; 
                                }
            
                }
          }
    
      print 'There were ' . $done . ' redirects deleted.' . "\n";
1
  • Better use loadByProperties instead of that nested double looping
    – Hudri
    Commented Mar 22, 2021 at 12:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.