T
T
TechNOIR2018-12-25 17:19:47
1C-Bitrix
TechNOIR, 2018-12-25 17:19:47

1C-Bitrix. How to massively and quickly enough delete all sections in the infoblock?

Good afternoon!
Please tell me how to massively and quickly enough delete all sections in the infoblock?
Total sections 55k
Through the admin panel everything hangs, despite the longer max execution time.
I sketched such a script, but somehow it works for a long time .. Or is this the norm?
thanks in advance

<?php
set_time_limit(60000);
// включаем вывод ошибочек
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
// включаем замер исполнения скрипта

// подключаем prolog bitrix 
require $_SERVER["DOCUMENT_ROOT"] . '/bitrix/modules/main/include/prolog_before.php';
// подключаем нужные модули
CModule::IncludeModule("iblock");
$infoblock = 4; // Инфоблок с ID ХХХ (необходимо установить ID нужного инфоблока)
$rs_Section = CIBlockSection::GetList(array('left_margin' => 'asc'), array('IBLOCK_ID' => $infoblock));
while ( $ar_Section = $rs_Section->Fetch() ) {
    $ar_Resu[] = array(  // собираем массив того, что нам нужно
        'ID' => $ar_Section['ID'], // id раздела
        'NAME' => $ar_Section['NAME'], // имя раздела (что нас, собственно, интересует)
        'IBLOCK_SECTION_ID' => $ar_Section['IBLOCK_SECTION_ID'],
    ); 
}

foreach ($ar_Resu as $section) {
   CIBlockElement::Delete($section["ID"]);
}
?>

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alexander, 2018-12-25
@idruweb

you can try to disable the recalculation of the tree or bang directly from the database

I
Ilya, 2018-12-25
@jasper-blondin

Has the script finished running?
As I understand it, you launched the script, and then wrote the question.
You can optimize as follows:
- add an array with array("ID") selection fields to the GetList method, so as not to pull values ​​from the database that are not needed
- execute the Delete method directly in the body of the while loop, so as not to collect the array unnecessarily
If still there is not enough time to execute, write a simple client-side with an AJAX request. It will work slowly, but it is guaranteed to complete the task and not time out.

R
Roman Gritsuk, 2018-12-25
@winer

With a large number of sections (30k+), Bitrix starts to put a spoke in the wheels for both the developer and the user who will work with such IB.
- Lists of IB elements will shamelessly slow down in the interface. On each hit, a section filter will be added.
- Editing them becomes very difficult, again due to the long loading of the interface and the long change time.
All this is due to the fact that sections are NESTED SETS trees. When adding/removing a section, LEFT_MARGIN and RIGHT_MARGIN are calculated for each section in the entire infoblock.
For adding and updating (CIBlockSection::Add, CIBlockSection::Update) there is a $bResort method parameter that allows you to disable this recalculation at the time of their execution.
Only after that it is MANDATORY to execute CIBlockSection::Resort.
This can be used with a large number of Update and Add operations. First do all the Update and Add operations with $bResort=false and then CIBlockSection::Resort
BUT!! Unfortunately, such parameter is not available for CIBlockSection::Delete. And this is where the tinkering begins.
Each call to CIBlockSection::Delete is:
and this is most likely not a complete list, but only what I can name from memory ....
You cannot quickly delete sections with the standard API without writing your own database queries that will do everything that is described above. Ideally, you need to take the code of the standard CIBlockSection::Delete, study it carefully and write your own method that will do the same thing using direct requests, but optimally.
If the question is to delete these sections in ANY amount of time, then you can write a page with step-by-step removal of sections, through ajax requests. Moreover, it should be borne in mind that at first the sections will be deleted VERY slowly and the ajax request may fail due to a timeout, so it will be necessary to delete a couple of sections in one step. And closer to the border of 10k sections, it will be possible to delete more in one step.

M
Michael Lyamin, 2018-12-28
@BusteR27

But judging by the code, you have collected an array of sections and then delete elements with these id.
One of the options is to turn off the indexing of the infoblock by the search module

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question