Title: Serialized data
Last modified: July 2, 2021

---

# Serialized data

 *  [julia77](https://wordpress.org/support/users/julia77/)
 * (@julia77)
 * [4 years, 11 months ago](https://wordpress.org/support/topic/serialized-data-5/)
 * Hello,
 * I´ve added a filter for not storing images metadata in db.
 * _add\_filter( ‘wp\_read\_image\_metadata’, ‘\_\_return\_false’ );_
 * Now I´d like to delete previously stored data but it´s serialized in wp_postmeta
   with meta_key value
 * _\_wp\_attachment\_metadata_
 * This is the array:
 *     ```
       [image_meta] => Array
               (
                   [aperture] => 0
                   [credit] => 
                   [camera] => 
                    => 
                   [created_timestamp] => 0
                   [copyright] => 
                   [focal_length] => 0
                   [iso] => 0
                   [shutter_speed] => 0
                   [title] => 
               )
       ```
   
 * I´ve read it´s not recommended to unserialize data in WP db manually. I don´t
   know why. If that´s true, is there a script or any other way to unserialize and
   delete these data?
 * Regards,
 * Julia
    -  This topic was modified 4 years, 11 months ago by [Yui](https://wordpress.org/support/users/fierevere/).
    -  This topic was modified 4 years, 11 months ago by [Jan Dembowski](https://wordpress.org/support/users/jdembowski/).
      Reason: Moved to Fixing WordPress, this is not an Everything else WordPress
      topic
    -  This topic was modified 4 years, 11 months ago by [Jose Castaneda](https://wordpress.org/support/users/jcastaneda/).
      Reason: fixed code

Viewing 8 replies - 1 through 8 (of 8 total)

 *  [Joy](https://wordpress.org/support/users/joyously/)
 * (@joyously)
 * [4 years, 11 months ago](https://wordpress.org/support/topic/serialized-data-5/#post-14612302)
 * You don’t need to unserialize it to delete it.
    Try one of the [Bulk Delete plugins](https://wordpress.org/plugins/search/bulk+delete/).
 *  Thread Starter [julia77](https://wordpress.org/support/users/julia77/)
 * (@julia77)
 * [4 years, 11 months ago](https://wordpress.org/support/topic/serialized-data-5/#post-14617690)
 * I´ve visited your link and didn´t find any plugin related with images metadata,
   even the pros versions (just attachments as a whole, not differentiating arrays).
 * Do you know why doing it manually is not recommended in WP db?
 *  [Joy](https://wordpress.org/support/users/joyously/)
 * (@joyously)
 * [4 years, 11 months ago](https://wordpress.org/support/topic/serialized-data-5/#post-14617986)
 * The plugins I was thinking of just delete things that match what you choose, 
   like posts or pages or post meta. I guess you are wanting to keep the post meta
   and just delete some parts of it.
    If you know how to read the serialized data,
   you can do it manually. It’s a hassle because of getting the lengths correct.
   Or you can write a small plugin to get the attachment data, delete the image_meta
   key, and save it. WP does the unserializing and serializing for you. [https://developer.wordpress.org/reference/functions/wp_get_attachment_metadata/](https://developer.wordpress.org/reference/functions/wp_get_attachment_metadata/)
   [https://developer.wordpress.org/reference/functions/wp_update_attachment_metadata/](https://developer.wordpress.org/reference/functions/wp_update_attachment_metadata/)
   There is also [https://developer.wordpress.org/reference/functions/wp_restore_image/](https://developer.wordpress.org/reference/functions/wp_restore_image/)
   if you change your mind.
 *  Thread Starter [julia77](https://wordpress.org/support/users/julia77/)
 * (@julia77)
 * [4 years, 11 months ago](https://wordpress.org/support/topic/serialized-data-5/#post-14620287)
 * [@joyously](https://wordpress.org/support/users/joyously/),
 * Yes, I´ve seen the function. But I don´t know a safe query to run in db for already
   stored metadata. If you read my first post in this topic, there´s a filter but
   previous data is already serialized.
 *  Moderator [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [4 years, 11 months ago](https://wordpress.org/support/topic/serialized-data-5/#post-14620870)
 * You don’t need to know, or care, what the metadata is to delete it.
 * Obviously make a backup first.
 * Next, take a look at the postmeta table. You will find that it consists of data
   stored by meta_key and meta_value. You only want to delete the data with the 
   meta_key = ‘_wp_attachment_metadata’. What the value is, well, you kinda don’t
   care if you’re just deleting it.
 * Now, if you want to read the data and save it somewhere else first, by reformatting
   it in some manner, then the function you want is `wp_get_attachment_metadata`.
   And it doesn’t return serialized data to you, it returns an array of the data.
   Already deserialized. This is true of serialized data in general, if you use 
   the WordPress built-in functions, then it handles that serialization stuff for
   you both in saving and in retrieving the data.
 * In other words, don’t run an SQL query yourself, instead write some PHP code 
   to loop through the attachment posts to get and reformat and resave your data
   in the way you want it, then run it in the WordPress context, as in a custom 
   made plugin.
 *  Thread Starter [julia77](https://wordpress.org/support/users/julia77/)
 * (@julia77)
 * [4 years, 11 months ago](https://wordpress.org/support/topic/serialized-data-5/#post-14620932)
 * [@otto42](https://wordpress.org/support/users/otto42/),
 * I don´t want to delete the entire meta_value, just part of it as [@joyously](https://wordpress.org/support/users/joyously/)
   understood. Is there a function to extract the key array ([image_meta]? I´ve 
   found this:
 * > `function maybe_unserialize( $data ) {
   >  if ( is_serialized( $data ) ) { return
   > @unserialize( trim( $data ) ); }
   >  return $data;
   >  }`
 * What do you think?
 *  [Joy](https://wordpress.org/support/users/joyously/)
 * (@joyously)
 * [4 years, 11 months ago](https://wordpress.org/support/topic/serialized-data-5/#post-14621316)
 * When you retrieve the attachment meta data with the WP function, WP deserializes
   it for you. You then use normal array manipulation on the data like
    `unset( 
   $data['image_data'] );` and then use the other WP function to write that back
   to the database.
 *  Thread Starter [julia77](https://wordpress.org/support/users/julia77/)
 * (@julia77)
 * [4 years, 10 months ago](https://wordpress.org/support/topic/serialized-data-5/#post-14661873)
 * Hello [@joyously](https://wordpress.org/support/users/joyously/),
 * Thanks for your response and sorry about my delay.
 * Could you provide this function syntaxis? For unserializing, then unsetting meta
   data and then writing it back to db?
 * Regards,
 * Julia

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘Serialized data’ is closed to new replies.

## Tags

 * [SERIALIZED DATA](https://wordpress.org/support/topic-tag/serialized-data/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 8 replies
 * 3 participants
 * Last reply from: [julia77](https://wordpress.org/support/users/julia77/)
 * Last activity: [4 years, 10 months ago](https://wordpress.org/support/topic/serialized-data-5/#post-14661873)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
