<p>If you need to do this on-demand, you could add a console controller to a custom module so that you can run it via command.</p>
<p>This page in the Craft docs explains how to set that up:
<a href="https://craftcms.com/docs/4.x/extend/commands.html" rel="nofollow noreferrer">https://craftcms.com/docs/4.x/extend/commands.html</a></p>
<p>For your controller method, you'd want to loop through the entries you want (using a <code>Entry::find()-&gt;section('section1,section2,...')-&gt;all()</code> query) and for each entry do the following:</p>
<pre><code>$entry-&gt;enabled = false;
$success = Craft::$app-&gt;elements-&gt;saveElement($entry);
if (!$success) {
   // Edit this fail condition as desired, such as outputting info without quitting.
   Craft::error('Couldn’t save the entry &quot;'.$entry-&gt;title.'&quot;', __METHOD__);
}
</code></pre>
<p>Run your custom CLI command in the console, and it'll disable each entry. If you have a TON of entries, you'll want the controller to instead call a queue job to prevent time-out.</p>
<p>Here's info on queue jobs:
<a href="https://craftcms.com/docs/4.x/extend/queue-jobs.html" rel="nofollow noreferrer">https://craftcms.com/docs/4.x/extend/queue-jobs.html</a></p>

CC BY-SA 4.0
2023-03-07T09:45:33.477Z
This is great! Hadn't thought about reusability. I'm going to need this a few more times probably, so I went with this. Great suggestion to use a job. Works like a charm!
7ochem
CC BY-SA 4.0
2023-03-08T00:32:39.340Z
Happy to help! Glad it went smoothly :)
cherrykoda
CC BY-SA 4.0
2023-03-07T05:12:07.697Z
2023-03-07T05:12:07.697Z
cherrykoda
<p>A <a href="https://craftcms.com/docs/4.x/extend/migrations.html#creating-migrations" rel="nofollow noreferrer">content migration</a> is probably the best approach to take. This way, any other modules/plugins that listen to entry events will be able to take the necessary action, and you can run and test it in development before deploying to production.</p>
<p>Here is the migration in its entirety. It should be straightforward enough to read, just be sure to swap out the site and section IDs.</p>
<pre class="lang-php prettyprint-override"><code>&lt;?php

namespace craft\contentmigrations;

use Craft;
use craft\db\Migration;
use craft\elements\Entry;

class m230306_120000_disable_entries extends Migration
{
    public function safeUp(): bool
    {
        $entries = Entry::find()
            -&gt;siteId([1, 2])
            -&gt;sectionId([1, 2, 3])
            -&gt;all();
        
        foreach ($entries as $entry) {
            // Set to `false` to disable, `true` to enable.
            $entry-&gt;enabled = false;
            Craft::$app-&gt;elements-&gt;saveElement($entry);
        }

        return true;
    }

    public function safeDown(): bool
    {
        return false;
    }
}
</code></pre>

CC BY-SA 4.0
2023-03-07T08:45:02.387Z
I have two sites. With `$entry->enabled = false;` does it disable for both/all sites? (= desired behaviour)
7ochem
CC BY-SA 4.0
2023-03-07T14:44:10.563Z
No, I added a site ID query param to my answer, which allows you to specify the sites to fetch entries from.
Ben Croker
CC BY-SA 4.0
2023-03-07T01:11:52.237Z
2023-03-07T14:43:26.470Z
2023-03-07T14:43:26.470Z
Ben Croker
<p>I need to disable all the entries of 3 (related) sections. After that I'll be running some imports and all actual data will be enabled again (within the correct sites). We're talking about around 2.000 entries per section.</p>
<p>In the admin/control panel I can only select all entries on the current page (per 100) and disable them. There's no option to disable all these entries. So through this method I should repeat this 20 times for each of the 3 sections...</p>
<p>Is there any other way to achieve this? Via the command line, through code or through an SQL query directly onto the database?</p>

<entries><migration><status>
How to disable all entries of certain section types?
# How to disable all entries of certain section types?

(Asked by: username_0 on 2023-03-06)

I need to disable all the entries of 3 (related) sections. After that I'll be running some imports and all actual data will be enabled again (within the correct sites). We're talking about around 2.000 entries per section.

In the admin/control panel I can only select all entries on the current page (per 100) and disable them. There's no option to disable all these entries. So through this method I should repeat this 20 times for each of the 3 sections...

Is there any other way to achieve this? Via the command line, through code or through an SQL query directly onto the database?

**Tags:** entries, migration, status

---

## 2 answers

### Answer by username_1 on 2023-03-07. Score: 3 | Accepted answer

If you need to do this on-demand, you could add a console controller to a custom module so that you can run it via command.
This page in the Craft docs explains how to set that up:
https://craftcms.com/docs/4.x/extend/commands.html
For your controller method, you'd want to loop through the entries you want (using a Entry::find()->section('section1,section2,...')->all() query) and for each entry do the following:
$entry->enabled = false;
$success = Craft::$app->elements->saveElement($entry);
if (!$success) {
   // Edit this fail condition as desired, such as outputting info without quitting.
   Craft::error('Couldn’t save the entry "'.$entry->title.'"', __METHOD__);
}

Run your custom CLI command in the console, and it'll disable each entry. If you have a TON of entries, you'll want the controller to instead call a queue job to prevent time-out.
Here's info on queue jobs:
https://craftcms.com/docs/4.x/extend/queue-jobs.html

> **Comments:**
> **username_0**: This is great! Hadn't thought about reusability. I'm going to need this a few more times probably, so I went with this. Great suggestion to use a job. Works like a charm!  
> **username_1**: Happy to help! Glad it went smoothly :)  

---

### Answer by username_2 on 2023-03-07. Score: 1

A content migration is probably the best approach to take. This way, any other modules/plugins that listen to entry events will be able to take the necessary action, and you can run and test it in development before deploying to production.
Here is the migration in its entirety. It should be straightforward enough to read, just be sure to swap out the site and section IDs.
siteId([1, 2])
            ->sectionId([1, 2, 3])
            ->all();
        
        foreach ($entries as $entry) {
            // Set to `false` to disable, `true` to enable.
            $entry->enabled = false;
            Craft::$app->elements->saveElement($entry);
        }

        return true;
    }

    public function safeDown(): bool
    {
        return false;
    }
}

> **Comments:**
> **username_0**: I have two sites. With `$entry->enabled = false;` does it disable for both/all sites? (= desired behaviour)  
> **username_2**: No, I added a site ID query param to my answer, which allows you to specify the sites to fetch entries from.  

---

