5

I created a block and put in some invalid PHP code resulted in entire site not accessible at all.

I'm fairly new to Drupal and I'm not a back-end programmer either. Much appreciated if you can provide a clear step-by-step guide on how to delete that particular block directly from the Database (PhpMyAdmin).

7
  • Login into PHPMy Admin with admin credentials
  • Search for the block table; in this table search for the block which you have created
  • Change the value of status column form 1 to 0
  • Delete all the content from the cache_bootstrap table
  • Thanks! I ended up following your guide since it's a step by step instruction. Downside is that if there's a lot of rows then would be nice to have a more efficient way of finding the block than going through it manually. – kyooriouskoala Jun 1 '12 at 4:11
  • 2
    To find rows in a database, you use SELECT; just use the right criteria to match the blocks you are looking for. Unfortunately, Drupal Answers doesn't aim to teach you how to use PHPMyAdmin, since it is off-topic. – kiamlaluno Jul 31 '14 at 21:21
  • This solution does not remove the block at all, it merely disables it. If you need to remove the block, see the answer below. – Erik Töyrä Silfverswärd Nov 23 '17 at 9:33
10

To delete a block programmatically using the Database API, try this:

$module = 'block';
$delta = 1;

// Get the block id (bid) required to delete the correct block from table 'block_custom'
$result = db_select('block', 'b')
         ->fields('b', array('bid'))
         ->condition('module', $module)
         ->condition('delta', $delta)
         ->execute();
$bid = $result->fetchField();

db_delete('block_custom')
  ->condition('bid', $bid)
  ->execute();

db_delete('block')
  ->condition('module', $module)
  ->condition('delta', $delta)
  ->execute();

db_delete('block_role')
  ->condition('module', $module)
  ->condition('delta', $delta)
  ->execute();

When creating custom blocks through the Drupal Block UI, the module name is block and the delta value is an integer. Be sure to use the correct delta value to delete the desired block.

  • 1
    Boom. Thanks, Tyler. I'd also add block_node_type to the list. : ) – Ryan Szrama Mar 8 '16 at 22:52
  • Warning! Using $delta to match against bid in table block_custom is very dangerous as you will delete another block. You should use the bid id from table block instead. – Erik Töyrä Silfverswärd Mar 17 '17 at 12:45
  • Updated the example to fix the problem. – Erik Töyrä Silfverswärd Mar 17 '17 at 12:59
  • @ErikTöyrä In my case it seems that using $delta to match against bid works just fine. Are you really sure it's problematic? – Miloš Kroulík Jan 29 '19 at 10:32
  • 1
    Hi, according to documentation api.drupal.org/api/drupal/includes!database!database.inc/… the second parameter of fields() method should be an array otherwise you will get an error: ->fields('b', array('bid')). – Fabrizio Fab01 Sabato Apr 4 '19 at 12:32
3

just use this simple query to delete the block.

delete from block where module = 'MODULE' and title = 'TITLE'

Replace MODULE with the name of the module creating the block, and TITLE with the block title.

If the block doesn't have a title, you could use only delete from block where module = 'MODULE'.

  • Thanks for sharing your solution :) What would be the query to search instead of delete? Can I replace delete with search or something like that? – kyooriouskoala Jun 1 '12 at 4:11
  • yes the query would be SELECT * FROM block WHERE module = 'MODULE' replace MODULE by the module creating the block i.e. search – AjitS Jun 1 '12 at 6:47
  • What if I don't know my block name? How does a block name translate into the database? Is everything in lowercase with underscores instead of spaces? – alouette Jan 7 '15 at 15:46
0

delete from block where delta = '[DELTA]';

If you created the block programmatically you should be able to recreate the delta value with the same logic you used to create it. It's the value you passed as the KEY to your blocks array for example $blocks['myblock'] -- myblock is the value stored in delta.

On an aside, I personally think its a good idea to prefix this name with your module prefix. When I look in my Blocks table, I can see mymodule_myblock for my delta and there is no ambiguity.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.