Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-03-07T09:45:33.477Z
Answers.Text: 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!
Answers.UserDisplayName: 7ochem
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-03-08T00:32:39.340Z
Answers.Text: Happy to help! Glad it went smoothly :)
Answers.UserDisplayName: cherrykoda
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-03-07T05:12:07.697Z
Answers.LastActivityDate: 2023-03-07T05:12:07.697Z
Answers.UserDisplayName: cherrykoda
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-03-07T08:45:02.387Z
Answers.Text: I have two sites. With `$entry->enabled = false;` does it disable for both/all sites? (= desired behaviour)
Answers.UserDisplayName: 7ochem
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-03-07T14:44:10.563Z
Answers.Text: No, I added a site ID query param to my answer, which allows you to specify the sites to fetch entries from.
Answers.UserDisplayName: Ben Croker
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-03-07T01:11:52.237Z
Answers.LastActivityDate: 2023-03-07T14:43:26.470Z
Answers.LastEditDate: 2023-03-07T14:43:26.470Z
Answers.UserDisplayName: Ben Croker
Body: <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>
  
Tags: <entries><migration><status>
Title: How to disable all entries of certain section types?
ThreadText: # 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](https://craftcms.com/docs/4.x/extend/migrations.html#creating-migrations) 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.
  
  ```php
  <?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()
              ->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.  
  
  ---
  
  