Drush command to delete default content

Created on 26 May 2022, about 2 years ago
Updated 27 May 2024, about 1 month ago

Problem/Motivation

Sometimes, we want to import default content and then disable the module, keeping the content.

Sometimes we want to bulk delete the content provided by this module.

Our preference is not to auto-delete on uninstall. Consider this use-case:

* Create custom_content module to import content to production
* custom_content requires default_content which requires hal and serialization
* We want the content, but do not want these 4 modules enabled
* We enable the module via drush and then disable it (and its dependencies)
* Content is present in production

The approach in #2844276: Option to automatically delete demo content β†’ would break that workflow.

During testing, however, we may want to import and delete the custom_content at will.

Steps to reproduce

n/a

Proposed resolution

* Create a drush command to read the info.yml files of default_content modules.
* Allow the drush command to delete all content or content of a specific entity type.

e.g. `drush default-content:delete my_test_content image`

Remaining tasks

Write the drush commands.

User interface changes

None, though this approach could be used to build a form later.

API changes

None anticipated.

Data model changes

None anticipated.

✨ Feature request
Status

Needs review

Version

2.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States agentrickard Georgia (US)

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ͺπŸ‡ΈSpain Eduardo Morales Alberti Spain, πŸ‡ͺπŸ‡Ί

    In our case, we tried to find all the UUIDs definitions on the default_content and remove them, it will remove all entities defined by default_content.
    We tried the drush command and the paragraphs were not removed for example.

    $entity_types = [
      'node',
      'paragraph',
      'taxonomy_term',
      'media',
      'file',
      'menu_link_content',
    ];
    
    $uuids = [];
    $module_folder = \Drupal::moduleHandler()
          ->getModule($module)
          ->getPath() . '/content';
    $dirs = array_filter(glob($module_folder . '/content/*'), 'is_dir');
    
    $uuids = [];
    $pattern = '/uuid: (\w.*-\w.*-\w.*-\w.*-\w.*)/m';
    foreach ($dirs as $dir) {
      $entity_type = basename($dir);
      if (!in_array($entity_type, $entity_types)) {
        continue;
      }
      $files = glob($dir . '/*.yml');
    
      foreach ($files as $file) {
        $contents = file_get_contents($file);
    
        if (preg_match_all($pattern, $contents, $matches, PREG_SET_ORDER, 0)) {
          foreach ($matches as $match) {
            $uuids[] = $match[1];
          }
        }
        $uuids = array_unique($uuids);
    
      }
    
    }
    
    foreach ($entity_types as $entity_type) {
      $entities = \Drupal::entityTypeManager()->getStorage($entity_type)->loadByProperties(['uuid' => $uuids]);
      \Drupal::entityTypeManager()->getStorage($entity_type)->delete($entities);
    }
    
    
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 8.1 & MySQL 5.7
    last update 3 months ago
    13 pass
  • πŸ‡ͺπŸ‡ΈSpain Eduardo Morales Alberti Spain, πŸ‡ͺπŸ‡Ί

    We created a new branch "3282547-drush-delete-content-reimport" to delete all entities from default_content based on all the uuids defined on the default_content module.
    Also we add a reimport command (deletes and import again the content).

  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 8.1 & MySQL 5.7
    last update 3 months ago
    13 pass
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 8.1 & MySQL 5.7
    last update 3 months ago
    13 pass
  • πŸ‡§πŸ‡―Benin delacosta456

    hi @eduardo-morales-alberti
    I was looking for something like this.
    The patch applied correctly but looks to have breaks another patch Drush comand to export all items from entity with defined criteria ✨ Drush comand to export all items from entity with defined criteria Needs review that i was using to export at once node+references to folder .

    Example : drush dcer node 34 --folder=modules/custom/mycustom_default_content/content

    after the merge request 45.patch

    the above command doesn't work and send the following error

    In LegacyServiceInstantiator.php line 282:
                                                                              
      You have requested a non-existent parameter "default_content.deleter".  
                                                                              

    Can you help
    Thanks

Production build 0.69.0 2024