Skip to main content
deleted 28 characters in body
Source Link
Brad Bell
  • 67.6k
  • 6
  • 75
  • 145

ImI'm trying to cleanupclean-up the assets in a specific folder that arentaren't being used at all, ie arenti.e. aren't related to anything.

I got the list okO.K. just using this

$assets = Asset::find()->volume('artists')->kind('image')->limit(50000)->ids();

    $assetsToDelete = [];

    foreach($assets as $asset) {
        $results = (new Query())
        ->select(['id'])
        ->from(['{{%relations}}'])
        ->where(['targetId' => $asset])
        ->orWhere(['sourceId' => $asset])
        ->all();

        if (!count($results)) {
            $assetsToDelete[]  = $asset;
        }
      
    }

    return  $this->asJson($assetsToDelete);

rightRight now imI'm just returning all the ids in an array but imI'm hoping to find a command to delete the element AND delete the stored asset on the server

Any ideas?

Im trying to cleanup the assets in a specific folder that arent being used at all, ie arent related to anything.

I got the list ok just using this

$assets = Asset::find()->volume('artists')->kind('image')->limit(50000)->ids();

    $assetsToDelete = [];

    foreach($assets as $asset) {
        $results = (new Query())
        ->select(['id'])
        ->from(['{{%relations}}'])
        ->where(['targetId' => $asset])
        ->orWhere(['sourceId' => $asset])
        ->all();

        if (!count($results)) {
            $assetsToDelete[]  = $asset;
        }
      
    }

    return  $this->asJson($assetsToDelete);

right now im just returning all the ids in an array but im hoping to find a command to delete the element AND delete the stored asset on the server

Any ideas?

I'm trying to clean-up the assets in a specific folder that aren't being used at all, i.e. aren't related to anything.

I got the list O.K. just using this

$assets = Asset::find()->volume('artists')->kind('image')->limit(50000)->ids();

$assetsToDelete = [];

foreach($assets as $asset) {
    $results = (new Query())
        ->select(['id'])
        ->from(['{{%relations}}'])
        ->where(['targetId' => $asset])
        ->orWhere(['sourceId' => $asset])
        ->all();

    if (!count($results)) {
        $assetsToDelete[]  = $asset;
    }     
}

return  $this->asJson($assetsToDelete);

Right now I'm just returning all the ids in an array but I'm hoping to find a command to delete the element AND delete the stored asset on the server

Any ideas?

Source Link
Keith Mancuso
  • 2.4k
  • 17
  • 28

How do I delete an asset with module or plugin

Im trying to cleanup the assets in a specific folder that arent being used at all, ie arent related to anything.

I got the list ok just using this

$assets = Asset::find()->volume('artists')->kind('image')->limit(50000)->ids();

    $assetsToDelete = [];

    foreach($assets as $asset) {
        $results = (new Query())
        ->select(['id'])
        ->from(['{{%relations}}'])
        ->where(['targetId' => $asset])
        ->orWhere(['sourceId' => $asset])
        ->all();

        if (!count($results)) {
            $assetsToDelete[]  = $asset;
        }
     
    }

    return  $this->asJson($assetsToDelete);

right now im just returning all the ids in an array but im hoping to find a command to delete the element AND delete the stored asset on the server

Any ideas?